blob: 7c0b6cc75d3d6ed90f471a0df032ebf290115899 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2002,2007-2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13#include <linux/uaccess.h>
14
15#include "kgsl.h"
16#include "kgsl_cffdump.h"
17#include "kgsl_sharedmem.h"
18
19#include "z180.h"
20#include "z180_reg.h"
21
22#define DRIVER_VERSION_MAJOR 3
23#define DRIVER_VERSION_MINOR 1
24
25#define Z180_DEVICE(device) \
26 KGSL_CONTAINER_OF(device, struct z180_device, dev)
27
28#define GSL_VGC_INT_MASK \
29 (REG_VGC_IRQSTATUS__MH_MASK | \
30 REG_VGC_IRQSTATUS__G2D_MASK | \
31 REG_VGC_IRQSTATUS__FIFO_MASK)
32
33#define VGV3_NEXTCMD_JUMP 0x01
34
35#define VGV3_NEXTCMD_NEXTCMD_FSHIFT 12
36#define VGV3_NEXTCMD_NEXTCMD_FMASK 0x7
37
38#define VGV3_CONTROL_MARKADD_FSHIFT 0
39#define VGV3_CONTROL_MARKADD_FMASK 0xfff
40
41#define Z180_PACKET_SIZE 15
42#define Z180_MARKER_SIZE 10
43#define Z180_CALL_CMD 0x1000
44#define Z180_MARKER_CMD 0x8000
45#define Z180_STREAM_END_CMD 0x9000
46#define Z180_STREAM_PACKET 0x7C000176
47#define Z180_STREAM_PACKET_CALL 0x7C000275
48#define Z180_PACKET_COUNT 8
49#define Z180_RB_SIZE (Z180_PACKET_SIZE*Z180_PACKET_COUNT \
50 *sizeof(uint32_t))
51
52#define NUMTEXUNITS 4
53#define TEXUNITREGCOUNT 25
54#define VG_REGCOUNT 0x39
55
56#define PACKETSIZE_BEGIN 3
57#define PACKETSIZE_G2DCOLOR 2
58#define PACKETSIZE_TEXUNIT (TEXUNITREGCOUNT * 2)
59#define PACKETSIZE_REG (VG_REGCOUNT * 2)
60#define PACKETSIZE_STATE (PACKETSIZE_TEXUNIT * NUMTEXUNITS + \
61 PACKETSIZE_REG + PACKETSIZE_BEGIN + \
62 PACKETSIZE_G2DCOLOR)
63#define PACKETSIZE_STATESTREAM (ALIGN((PACKETSIZE_STATE * \
64 sizeof(unsigned int)), 32) / \
65 sizeof(unsigned int))
66
67#define Z180_INVALID_CONTEXT UINT_MAX
68
69/* z180 MH arbiter config*/
70#define Z180_CFG_MHARB \
71 (0x10 \
72 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
73 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
74 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
75 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
76 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
77 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
78 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
79 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
80 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
81 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
82 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
83 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
84 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
85 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
86
87#define Z180_TIMESTAMP_EPSILON 20000
88#define Z180_IDLE_COUNT_MAX 1000000
89
90enum z180_cmdwindow_type {
91 Z180_CMDWINDOW_2D = 0x00000000,
92 Z180_CMDWINDOW_MMU = 0x00000002,
93};
94
95#define Z180_CMDWINDOW_TARGET_MASK 0x000000FF
96#define Z180_CMDWINDOW_ADDR_MASK 0x00FFFF00
97#define Z180_CMDWINDOW_TARGET_SHIFT 0
98#define Z180_CMDWINDOW_ADDR_SHIFT 8
99
100static int z180_start(struct kgsl_device *device, unsigned int init_ram);
101static int z180_stop(struct kgsl_device *device);
102static int z180_wait(struct kgsl_device *device,
103 unsigned int timestamp,
104 unsigned int msecs);
105static void z180_regread(struct kgsl_device *device,
106 unsigned int offsetwords,
107 unsigned int *value);
108static void z180_regwrite(struct kgsl_device *device,
109 unsigned int offsetwords,
110 unsigned int value);
111static void z180_cmdwindow_write(struct kgsl_device *device,
112 unsigned int addr,
113 unsigned int data);
114
115#define Z180_MMU_CONFIG \
116 (0x01 \
117 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
118 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
119 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
120 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
121 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
122 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
123 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
124 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
125 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
126 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
127 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
128
129static const struct kgsl_functable z180_functable;
130
131static struct z180_device device_2d0 = {
132 .dev = {
133 .name = DEVICE_2D0_NAME,
134 .id = KGSL_DEVICE_2D0,
135 .ver_major = DRIVER_VERSION_MAJOR,
136 .ver_minor = DRIVER_VERSION_MINOR,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600137 .mh = {
138 .mharb = Z180_CFG_MHARB,
139 .mh_intf_cfg1 = 0x00032f07,
140 .mh_intf_cfg2 = 0x004b274f,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700141 /* turn off memory protection unit by setting
142 acceptable physical address range to include
143 all pages. */
144 .mpu_base = 0x00000000,
145 .mpu_range = 0xFFFFF000,
146 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600147 .mmu = {
148 .config = Z180_MMU_CONFIG,
149 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150 .pwrctrl = {
151 .regulator_name = "fs_gfx2d0",
152 .irq_name = KGSL_2D0_IRQ,
153 },
154 .mutex = __MUTEX_INITIALIZER(device_2d0.dev.mutex),
155 .state = KGSL_STATE_INIT,
156 .active_cnt = 0,
157 .iomemname = KGSL_2D0_REG_MEMORY,
158 .ftbl = &z180_functable,
159 .display_off = {
160#ifdef CONFIG_HAS_EARLYSUSPEND
161 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
162 .suspend = kgsl_early_suspend_driver,
163 .resume = kgsl_late_resume_driver,
164#endif
165 },
166 },
167};
168
169static struct z180_device device_2d1 = {
170 .dev = {
171 .name = DEVICE_2D1_NAME,
172 .id = KGSL_DEVICE_2D1,
173 .ver_major = DRIVER_VERSION_MAJOR,
174 .ver_minor = DRIVER_VERSION_MINOR,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600175 .mh = {
176 .mharb = Z180_CFG_MHARB,
177 .mh_intf_cfg1 = 0x00032f07,
178 .mh_intf_cfg2 = 0x004b274f,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179 /* turn off memory protection unit by setting
180 acceptable physical address range to include
181 all pages. */
182 .mpu_base = 0x00000000,
183 .mpu_range = 0xFFFFF000,
184 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600185 .mmu = {
186 .config = Z180_MMU_CONFIG,
187 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188 .pwrctrl = {
189 .regulator_name = "fs_gfx2d1",
190 .irq_name = KGSL_2D1_IRQ,
191 },
192 .mutex = __MUTEX_INITIALIZER(device_2d1.dev.mutex),
193 .state = KGSL_STATE_INIT,
194 .active_cnt = 0,
195 .iomemname = KGSL_2D1_REG_MEMORY,
196 .ftbl = &z180_functable,
197 .display_off = {
198#ifdef CONFIG_HAS_EARLYSUSPEND
199 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
200 .suspend = kgsl_early_suspend_driver,
201 .resume = kgsl_late_resume_driver,
202#endif
203 },
204 },
205};
206
207static irqreturn_t z180_isr(int irq, void *data)
208{
209 irqreturn_t result = IRQ_NONE;
210 unsigned int status;
211 struct kgsl_device *device = (struct kgsl_device *) data;
212 struct z180_device *z180_dev = Z180_DEVICE(device);
213
214 z180_regread(device, ADDR_VGC_IRQSTATUS >> 2, &status);
215
216 if (status & GSL_VGC_INT_MASK) {
217 z180_regwrite(device,
218 ADDR_VGC_IRQSTATUS >> 2, status & GSL_VGC_INT_MASK);
219
220 result = IRQ_HANDLED;
221
222 if (status & REG_VGC_IRQSTATUS__FIFO_MASK)
223 KGSL_DRV_ERR(device, "z180 fifo interrupt\n");
224 if (status & REG_VGC_IRQSTATUS__MH_MASK)
225 kgsl_mh_intrcallback(device);
226 if (status & REG_VGC_IRQSTATUS__G2D_MASK) {
227 int count;
228
229 z180_regread(device,
230 ADDR_VGC_IRQ_ACTIVE_CNT >> 2,
231 &count);
232
233 count >>= 8;
234 count &= 255;
235 z180_dev->timestamp += count;
236
237 wake_up_interruptible(&device->wait_queue);
238
239 atomic_notifier_call_chain(
240 &(device->ts_notifier_list),
241 device->id, NULL);
242 }
243 }
244
245 if ((device->pwrctrl.nap_allowed == true) &&
246 (device->requested_state == KGSL_STATE_NONE)) {
247 device->requested_state = KGSL_STATE_NAP;
248 queue_work(device->work_queue, &device->idle_check_ws);
249 }
250 mod_timer(&device->idle_timer,
251 jiffies + device->pwrctrl.interval_timeout);
252
253 return result;
254}
255
256static int z180_cleanup_pt(struct kgsl_device *device,
257 struct kgsl_pagetable *pagetable)
258{
259 struct z180_device *z180_dev = Z180_DEVICE(device);
260
261 kgsl_mmu_unmap(pagetable, &device->mmu.dummyspace);
262
263 kgsl_mmu_unmap(pagetable, &device->memstore);
264
265 kgsl_mmu_unmap(pagetable, &z180_dev->ringbuffer.cmdbufdesc);
266
267 return 0;
268}
269
270static int z180_setup_pt(struct kgsl_device *device,
271 struct kgsl_pagetable *pagetable)
272{
273 int result = 0;
274 struct z180_device *z180_dev = Z180_DEVICE(device);
275
276 result = kgsl_mmu_map_global(pagetable, &device->mmu.dummyspace,
277 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
278
279 if (result)
280 goto error;
281
282 result = kgsl_mmu_map_global(pagetable, &device->memstore,
283 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
284 if (result)
285 goto error_unmap_dummy;
286
287 result = kgsl_mmu_map_global(pagetable,
288 &z180_dev->ringbuffer.cmdbufdesc,
289 GSL_PT_PAGE_RV);
290 if (result)
291 goto error_unmap_memstore;
292 return result;
293
294error_unmap_dummy:
295 kgsl_mmu_unmap(pagetable, &device->mmu.dummyspace);
296
297error_unmap_memstore:
298 kgsl_mmu_unmap(pagetable, &device->memstore);
299
300error:
301 return result;
302}
303
304static inline unsigned int rb_offset(unsigned int index)
305{
306 return index*sizeof(unsigned int)*(Z180_PACKET_SIZE);
307}
308
309static void addmarker(struct z180_ringbuffer *rb, unsigned int index)
310{
311 char *ptr = (char *)(rb->cmdbufdesc.hostptr);
312 unsigned int *p = (unsigned int *)(ptr + rb_offset(index));
313
314 *p++ = Z180_STREAM_PACKET;
315 *p++ = (Z180_MARKER_CMD | 5);
316 *p++ = ADDR_VGV3_LAST << 24;
317 *p++ = ADDR_VGV3_LAST << 24;
318 *p++ = ADDR_VGV3_LAST << 24;
319 *p++ = Z180_STREAM_PACKET;
320 *p++ = 5;
321 *p++ = ADDR_VGV3_LAST << 24;
322 *p++ = ADDR_VGV3_LAST << 24;
323 *p++ = ADDR_VGV3_LAST << 24;
324}
325
326static void addcmd(struct z180_ringbuffer *rb, unsigned int index,
327 unsigned int cmd, unsigned int nextcnt)
328{
329 char * ptr = (char *)(rb->cmdbufdesc.hostptr);
330 unsigned int *p = (unsigned int *)(ptr + (rb_offset(index)
331 + (Z180_MARKER_SIZE * sizeof(unsigned int))));
332
333 *p++ = Z180_STREAM_PACKET_CALL;
334 *p++ = cmd;
335 *p++ = Z180_CALL_CMD | nextcnt;
336 *p++ = ADDR_VGV3_LAST << 24;
337 *p++ = ADDR_VGV3_LAST << 24;
338}
339
340static void z180_cmdstream_start(struct kgsl_device *device)
341{
342 struct z180_device *z180_dev = Z180_DEVICE(device);
343 unsigned int cmd = VGV3_NEXTCMD_JUMP << VGV3_NEXTCMD_NEXTCMD_FSHIFT;
344
345 z180_dev->timestamp = 0;
346 z180_dev->current_timestamp = 0;
347
348 addmarker(&z180_dev->ringbuffer, 0);
349
350 z180_cmdwindow_write(device, ADDR_VGV3_MODE, 4);
351
352 z180_cmdwindow_write(device, ADDR_VGV3_NEXTADDR,
353 z180_dev->ringbuffer.cmdbufdesc.gpuaddr);
354
355 z180_cmdwindow_write(device, ADDR_VGV3_NEXTCMD, cmd | 5);
356
357 z180_cmdwindow_write(device, ADDR_VGV3_WRITEADDR,
358 device->memstore.gpuaddr);
359
360 cmd = (int)(((1) & VGV3_CONTROL_MARKADD_FMASK)
361 << VGV3_CONTROL_MARKADD_FSHIFT);
362
363 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, cmd);
364
365 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, 0);
366}
367
368static int room_in_rb(struct z180_device *device)
369{
370 int ts_diff;
371
372 ts_diff = device->current_timestamp - device->timestamp;
373
374 return ts_diff < Z180_PACKET_COUNT;
375}
376
377static int z180_idle(struct kgsl_device *device, unsigned int timeout)
378{
379 int status = 0;
380 struct z180_device *z180_dev = Z180_DEVICE(device);
381
382 if (z180_dev->current_timestamp > z180_dev->timestamp)
383 status = z180_wait(device, z180_dev->current_timestamp,
384 timeout);
385
386 if (status)
387 KGSL_DRV_ERR(device, "z180_waittimestamp() timed out\n");
388
389 return status;
390}
391
392static void z180_setstate(struct kgsl_device *device, uint32_t flags)
393{
394 kgsl_default_setstate(device, flags);
395}
396
397int
398z180_cmdstream_issueibcmds(struct kgsl_device_private *dev_priv,
399 struct kgsl_context *context,
400 struct kgsl_ibdesc *ibdesc,
401 unsigned int numibs,
402 uint32_t *timestamp,
403 unsigned int ctrl)
404{
405 unsigned int result = 0;
406 unsigned int ofs = PACKETSIZE_STATESTREAM * sizeof(unsigned int);
407 unsigned int cnt = 5;
408 unsigned int nextaddr = 0;
409 unsigned int index = 0;
410 unsigned int nextindex;
411 unsigned int nextcnt = Z180_STREAM_END_CMD | 5;
412 struct kgsl_memdesc tmp = {0};
413 unsigned int cmd;
414 struct kgsl_device *device = dev_priv->device;
415 struct kgsl_pagetable *pagetable = dev_priv->process_priv->pagetable;
416 struct z180_device *z180_dev = Z180_DEVICE(device);
417 unsigned int sizedwords;
418
419 if (device->state & KGSL_STATE_HUNG) {
420 return -EINVAL;
421 goto error;
422 }
423 if (numibs != 1) {
424 KGSL_DRV_ERR(device, "Invalid number of ibs: %d\n", numibs);
425 result = -EINVAL;
426 goto error;
427 }
428 cmd = ibdesc[0].gpuaddr;
429 sizedwords = ibdesc[0].sizedwords;
430
431 tmp.hostptr = (void *)*timestamp;
432
433 KGSL_CMD_INFO(device, "ctxt %d ibaddr 0x%08x sizedwords %d\n",
434 context->id, cmd, sizedwords);
435 /* context switch */
436 if ((context->id != (int)z180_dev->ringbuffer.prevctx) ||
437 (ctrl & KGSL_CONTEXT_CTX_SWITCH)) {
438 KGSL_CMD_INFO(device, "context switch %d -> %d\n",
439 context->id, z180_dev->ringbuffer.prevctx);
440 kgsl_mmu_setstate(device, pagetable);
441 cnt = PACKETSIZE_STATESTREAM;
442 ofs = 0;
443 }
444 z180_setstate(device, kgsl_pt_get_flags(device->mmu.hwpagetable,
445 device->id));
446
447 result = wait_event_interruptible_timeout(device->wait_queue,
448 room_in_rb(z180_dev),
449 msecs_to_jiffies(KGSL_TIMEOUT_DEFAULT));
450 if (result < 0) {
451 KGSL_CMD_ERR(device, "wait_event_interruptible_timeout "
452 "failed: %d\n", result);
453 goto error;
454 }
455 result = 0;
456
457 index = z180_dev->current_timestamp % Z180_PACKET_COUNT;
458 z180_dev->current_timestamp++;
459 nextindex = z180_dev->current_timestamp % Z180_PACKET_COUNT;
460 *timestamp = z180_dev->current_timestamp;
461
462 z180_dev->ringbuffer.prevctx = context->id;
463
464 addcmd(&z180_dev->ringbuffer, index, cmd + ofs, cnt);
465
466 /* Make sure the next ringbuffer entry has a marker */
467 addmarker(&z180_dev->ringbuffer, nextindex);
468
469 nextaddr = z180_dev->ringbuffer.cmdbufdesc.gpuaddr
470 + rb_offset(nextindex);
471
472 tmp.hostptr = (void *)(tmp.hostptr +
473 (sizedwords * sizeof(unsigned int)));
474 tmp.size = 12;
475
476 kgsl_sharedmem_writel(&tmp, 4, nextaddr);
477 kgsl_sharedmem_writel(&tmp, 8, nextcnt);
478
479 /* sync memory before activating the hardware for the new command*/
480 mb();
481
482 cmd = (int)(((2) & VGV3_CONTROL_MARKADD_FMASK)
483 << VGV3_CONTROL_MARKADD_FSHIFT);
484
485 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, cmd);
486 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, 0);
487error:
488 return result;
489}
490
491static int z180_ringbuffer_init(struct kgsl_device *device)
492{
493 struct z180_device *z180_dev = Z180_DEVICE(device);
494 memset(&z180_dev->ringbuffer, 0, sizeof(struct z180_ringbuffer));
495 z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT;
496 return kgsl_allocate_contiguous(&z180_dev->ringbuffer.cmdbufdesc,
497 Z180_RB_SIZE);
498}
499
500static void z180_ringbuffer_close(struct kgsl_device *device)
501{
502 struct z180_device *z180_dev = Z180_DEVICE(device);
503 kgsl_sharedmem_free(&z180_dev->ringbuffer.cmdbufdesc);
504 memset(&z180_dev->ringbuffer, 0, sizeof(struct z180_ringbuffer));
505}
506
507static int __devinit z180_probe(struct platform_device *pdev)
508{
509 int status = -EINVAL;
510 struct kgsl_device *device = NULL;
511 struct z180_device *z180_dev;
512
513 device = (struct kgsl_device *)pdev->id_entry->driver_data;
514 device->parentdev = &pdev->dev;
515
516 z180_dev = Z180_DEVICE(device);
517 spin_lock_init(&z180_dev->cmdwin_lock);
518
519 status = z180_ringbuffer_init(device);
520 if (status != 0)
521 goto error;
522
523 status = kgsl_device_platform_probe(device, z180_isr);
524 if (status)
525 goto error_close_ringbuffer;
526
527 return status;
528
529error_close_ringbuffer:
530 z180_ringbuffer_close(device);
531error:
532 device->parentdev = NULL;
533 return status;
534}
535
536static int __devexit z180_remove(struct platform_device *pdev)
537{
538 struct kgsl_device *device = NULL;
539
540 device = (struct kgsl_device *)pdev->id_entry->driver_data;
541
542 kgsl_device_platform_remove(device);
543
544 z180_ringbuffer_close(device);
545
546 return 0;
547}
548
549static int z180_start(struct kgsl_device *device, unsigned int init_ram)
550{
551 int status = 0;
552
553 device->state = KGSL_STATE_INIT;
554 device->requested_state = KGSL_STATE_NONE;
555 KGSL_PWR_WARN(device, "state -> INIT, device %d\n", device->id);
556
557 kgsl_pwrctrl_enable(device);
558
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559 /* Set interrupts to 0 to ensure a good state */
560 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0x0);
561
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600562 kgsl_mh_start(device);
563
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564 status = kgsl_mmu_start(device);
565 if (status)
566 goto error_clk_off;
567
568 z180_cmdstream_start(device);
569
570 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
571 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_IRQ_ON);
572 return 0;
573
574error_clk_off:
575 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0);
576 kgsl_pwrctrl_disable(device);
577 return status;
578}
579
580static int z180_stop(struct kgsl_device *device)
581{
582 z180_idle(device, KGSL_TIMEOUT_DEFAULT);
583
Jeremy Gebben1757a852011-07-11 16:04:38 -0600584 del_timer_sync(&device->idle_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585
586 kgsl_mmu_stop(device);
587
588 /* Disable the clocks before the power rail. */
589 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
590
591 kgsl_pwrctrl_disable(device);
592
593 return 0;
594}
595
596static int z180_getproperty(struct kgsl_device *device,
597 enum kgsl_property_type type,
598 void *value,
599 unsigned int sizebytes)
600{
601 int status = -EINVAL;
602
603 switch (type) {
604 case KGSL_PROP_DEVICE_INFO:
605 {
606 struct kgsl_devinfo devinfo;
607
608 if (sizebytes != sizeof(devinfo)) {
609 status = -EINVAL;
610 break;
611 }
612
613 memset(&devinfo, 0, sizeof(devinfo));
614 devinfo.device_id = device->id+1;
615 devinfo.chip_id = 0;
616 devinfo.mmu_enabled = kgsl_mmu_enabled();
617
618 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
619 0) {
620 status = -EFAULT;
621 break;
622 }
623 status = 0;
624 }
625 break;
626 case KGSL_PROP_MMU_ENABLE:
627 {
628#ifdef CONFIG_MSM_KGSL_MMU
629 int mmuProp = 1;
630#else
631 int mmuProp = 0;
632#endif
633 if (sizebytes != sizeof(int)) {
634 status = -EINVAL;
635 break;
636 }
637 if (copy_to_user(value, &mmuProp, sizeof(mmuProp))) {
638 status = -EFAULT;
639 break;
640 }
641 status = 0;
642 }
643 break;
644
645 default:
646 KGSL_DRV_ERR(device, "invalid property: %d\n", type);
647 status = -EINVAL;
648 }
649 return status;
650}
651
652static unsigned int z180_isidle(struct kgsl_device *device)
653{
654 int status = false;
655 struct z180_device *z180_dev = Z180_DEVICE(device);
656
657 int timestamp = z180_dev->timestamp;
658
659 if (timestamp == z180_dev->current_timestamp)
660 status = true;
661
662 return status;
663}
664
665static int z180_suspend_context(struct kgsl_device *device)
666{
667 struct z180_device *z180_dev = Z180_DEVICE(device);
668
669 z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT;
670
671 return 0;
672}
673
674/* Not all Z180 registers are directly accessible.
675 * The _z180_(read|write)_simple functions below handle the ones that are.
676 */
677static void _z180_regread_simple(struct kgsl_device *device,
678 unsigned int offsetwords,
679 unsigned int *value)
680{
681 unsigned int *reg;
682
683 BUG_ON(offsetwords * sizeof(uint32_t) >= device->regspace.sizebytes);
684
685 reg = (unsigned int *)(device->regspace.mmio_virt_base
686 + (offsetwords << 2));
687
688 /*ensure this read finishes before the next one.
689 * i.e. act like normal readl() */
690 *value = __raw_readl(reg);
691 rmb();
692
693}
694
695static void _z180_regwrite_simple(struct kgsl_device *device,
696 unsigned int offsetwords,
697 unsigned int value)
698{
699 unsigned int *reg;
700
701 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
702
703 reg = (unsigned int *)(device->regspace.mmio_virt_base
704 + (offsetwords << 2));
705 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
706 /*ensure previous writes post before this one,
707 * i.e. act like normal writel() */
708 wmb();
709 __raw_writel(value, reg);
710}
711
712
713/* The MH registers must be accessed through via a 2 step write, (read|write)
714 * process. These registers may be accessed from interrupt context during
715 * the handling of MH or MMU error interrupts. Therefore a spin lock is used
716 * to ensure that the 2 step sequence is not interrupted.
717 */
718static void _z180_regread_mmu(struct kgsl_device *device,
719 unsigned int offsetwords,
720 unsigned int *value)
721{
722 struct z180_device *z180_dev = Z180_DEVICE(device);
723 unsigned long flags;
724
725 spin_lock_irqsave(&z180_dev->cmdwin_lock, flags);
726 _z180_regwrite_simple(device, (ADDR_VGC_MH_READ_ADDR >> 2),
727 offsetwords);
728 _z180_regread_simple(device, (ADDR_VGC_MH_DATA_ADDR >> 2), value);
729 spin_unlock_irqrestore(&z180_dev->cmdwin_lock, flags);
730}
731
732
733static void _z180_regwrite_mmu(struct kgsl_device *device,
734 unsigned int offsetwords,
735 unsigned int value)
736{
737 struct z180_device *z180_dev = Z180_DEVICE(device);
738 unsigned int cmdwinaddr;
739 unsigned long flags;
740
741 cmdwinaddr = ((Z180_CMDWINDOW_MMU << Z180_CMDWINDOW_TARGET_SHIFT) &
742 Z180_CMDWINDOW_TARGET_MASK);
743 cmdwinaddr |= ((offsetwords << Z180_CMDWINDOW_ADDR_SHIFT) &
744 Z180_CMDWINDOW_ADDR_MASK);
745
746 spin_lock_irqsave(&z180_dev->cmdwin_lock, flags);
747 _z180_regwrite_simple(device, ADDR_VGC_MMUCOMMANDSTREAM >> 2,
748 cmdwinaddr);
749 _z180_regwrite_simple(device, ADDR_VGC_MMUCOMMANDSTREAM >> 2, value);
750 spin_unlock_irqrestore(&z180_dev->cmdwin_lock, flags);
751}
752
753/* the rest of the code doesn't want to think about if it is writing mmu
754 * registers or normal registers so handle it here
755 */
756static void z180_regread(struct kgsl_device *device,
757 unsigned int offsetwords,
758 unsigned int *value)
759{
760 if (!in_interrupt())
761 kgsl_pre_hwaccess(device);
762
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600763 if ((offsetwords >= MH_ARBITER_CONFIG &&
764 offsetwords <= MH_AXI_HALT_CONTROL) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765 (offsetwords >= MH_MMU_CONFIG &&
766 offsetwords <= MH_MMU_MPU_END)) {
767 _z180_regread_mmu(device, offsetwords, value);
768 } else {
769 _z180_regread_simple(device, offsetwords, value);
770 }
771}
772
773static void z180_regwrite(struct kgsl_device *device,
774 unsigned int offsetwords,
775 unsigned int value)
776{
777 if (!in_interrupt())
778 kgsl_pre_hwaccess(device);
779
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600780 if ((offsetwords >= MH_ARBITER_CONFIG &&
781 offsetwords <= MH_CLNT_INTF_CTRL_CONFIG2) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782 (offsetwords >= MH_MMU_CONFIG &&
783 offsetwords <= MH_MMU_MPU_END)) {
784 _z180_regwrite_mmu(device, offsetwords, value);
785 } else {
786 _z180_regwrite_simple(device, offsetwords, value);
787 }
788}
789
790static void z180_cmdwindow_write(struct kgsl_device *device,
791 unsigned int addr, unsigned int data)
792{
793 unsigned int cmdwinaddr;
794
795 cmdwinaddr = ((Z180_CMDWINDOW_2D << Z180_CMDWINDOW_TARGET_SHIFT) &
796 Z180_CMDWINDOW_TARGET_MASK);
797 cmdwinaddr |= ((addr << Z180_CMDWINDOW_ADDR_SHIFT) &
798 Z180_CMDWINDOW_ADDR_MASK);
799
800 z180_regwrite(device, ADDR_VGC_COMMANDSTREAM >> 2, cmdwinaddr);
801 z180_regwrite(device, ADDR_VGC_COMMANDSTREAM >> 2, data);
802}
803
804static unsigned int z180_readtimestamp(struct kgsl_device *device,
805 enum kgsl_timestamp_type type)
806{
807 struct z180_device *z180_dev = Z180_DEVICE(device);
808 /* get current EOP timestamp */
809 return z180_dev->timestamp;
810}
811
812static int z180_waittimestamp(struct kgsl_device *device,
813 unsigned int timestamp,
814 unsigned int msecs)
815{
816 int status = -EINVAL;
817 mutex_unlock(&device->mutex);
818 status = z180_wait(device, timestamp, msecs);
819 mutex_lock(&device->mutex);
820
821 return status;
822}
823
824static int z180_wait(struct kgsl_device *device,
825 unsigned int timestamp,
826 unsigned int msecs)
827{
828 int status = -EINVAL;
829 long timeout = 0;
830
831 timeout = wait_io_event_interruptible_timeout(
832 device->wait_queue,
833 kgsl_check_timestamp(device, timestamp),
834 msecs_to_jiffies(msecs));
835
836 if (timeout > 0)
837 status = 0;
838 else if (timeout == 0) {
839 status = -ETIMEDOUT;
840 device->state = KGSL_STATE_HUNG;
841 KGSL_PWR_WARN(device, "state -> HUNG, device %d\n", device->id);
842 } else
843 status = timeout;
844
845 return status;
846}
847
848static void
849z180_drawctxt_destroy(struct kgsl_device *device,
850 struct kgsl_context *context)
851{
852 struct z180_device *z180_dev = Z180_DEVICE(device);
853
854 z180_idle(device, KGSL_TIMEOUT_DEFAULT);
855
856 if (z180_dev->ringbuffer.prevctx == context->id) {
857 z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT;
858 device->mmu.hwpagetable = device->mmu.defaultpagetable;
859 kgsl_setstate(device, KGSL_MMUFLAGS_PTUPDATE);
860 }
861}
862
863static void z180_power_stats(struct kgsl_device *device,
864 struct kgsl_power_stats *stats)
865{
866 stats->total_time = 0;
867 stats->busy_time = 0;
868}
869
870static void z180_irqctrl(struct kgsl_device *device, int state)
871{
872 /* Control interrupts for Z180 and the Z180 MMU */
873
874 if (state) {
875 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 3);
876 z180_regwrite(device, MH_INTERRUPT_MASK, KGSL_MMU_INT_MASK);
877 } else {
878 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0);
879 z180_regwrite(device, MH_INTERRUPT_MASK, 0);
880 }
881}
882
883static const struct kgsl_functable z180_functable = {
884 /* Mandatory functions */
885 .regread = z180_regread,
886 .regwrite = z180_regwrite,
887 .idle = z180_idle,
888 .isidle = z180_isidle,
889 .suspend_context = z180_suspend_context,
890 .start = z180_start,
891 .stop = z180_stop,
892 .getproperty = z180_getproperty,
893 .waittimestamp = z180_waittimestamp,
894 .readtimestamp = z180_readtimestamp,
895 .issueibcmds = z180_cmdstream_issueibcmds,
896 .setup_pt = z180_setup_pt,
897 .cleanup_pt = z180_cleanup_pt,
898 .power_stats = z180_power_stats,
899 .irqctrl = z180_irqctrl,
900 /* Optional functions */
901 .setstate = z180_setstate,
902 .drawctxt_create = NULL,
903 .drawctxt_destroy = z180_drawctxt_destroy,
904 .ioctl = NULL,
905};
906
907static struct platform_device_id z180_id_table[] = {
908 { DEVICE_2D0_NAME, (kernel_ulong_t)&device_2d0.dev, },
909 { DEVICE_2D1_NAME, (kernel_ulong_t)&device_2d1.dev, },
910 { },
911};
912MODULE_DEVICE_TABLE(platform, z180_id_table);
913
914static struct platform_driver z180_platform_driver = {
915 .probe = z180_probe,
916 .remove = __devexit_p(z180_remove),
917 .suspend = kgsl_suspend_driver,
918 .resume = kgsl_resume_driver,
919 .id_table = z180_id_table,
920 .driver = {
921 .owner = THIS_MODULE,
922 .name = DEVICE_2D_NAME,
923 .pm = &kgsl_pm_ops,
924 }
925};
926
927static int __init kgsl_2d_init(void)
928{
929 return platform_driver_register(&z180_platform_driver);
930}
931
932static void __exit kgsl_2d_exit(void)
933{
934 platform_driver_unregister(&z180_platform_driver);
935}
936
937module_init(kgsl_2d_init);
938module_exit(kgsl_2d_exit);
939
940MODULE_DESCRIPTION("2D Graphics driver");
941MODULE_VERSION("1.2");
942MODULE_LICENSE("GPL v2");
943MODULE_ALIAS("platform:kgsl_2d");