blob: cc57115ab3a84588daef981e35cba676b0674add [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,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -0600160 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
162 .suspend = kgsl_early_suspend_driver,
163 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700164 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600165#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166 },
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
Jordan Crouse9f739212011-07-28 08:37:57 -0600256static void z180_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257 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);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266}
267
268static int z180_setup_pt(struct kgsl_device *device,
269 struct kgsl_pagetable *pagetable)
270{
271 int result = 0;
272 struct z180_device *z180_dev = Z180_DEVICE(device);
273
274 result = kgsl_mmu_map_global(pagetable, &device->mmu.dummyspace,
275 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
276
277 if (result)
278 goto error;
279
280 result = kgsl_mmu_map_global(pagetable, &device->memstore,
281 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
282 if (result)
283 goto error_unmap_dummy;
284
285 result = kgsl_mmu_map_global(pagetable,
286 &z180_dev->ringbuffer.cmdbufdesc,
287 GSL_PT_PAGE_RV);
288 if (result)
289 goto error_unmap_memstore;
290 return result;
291
292error_unmap_dummy:
293 kgsl_mmu_unmap(pagetable, &device->mmu.dummyspace);
294
295error_unmap_memstore:
296 kgsl_mmu_unmap(pagetable, &device->memstore);
297
298error:
299 return result;
300}
301
302static inline unsigned int rb_offset(unsigned int index)
303{
304 return index*sizeof(unsigned int)*(Z180_PACKET_SIZE);
305}
306
307static void addmarker(struct z180_ringbuffer *rb, unsigned int index)
308{
309 char *ptr = (char *)(rb->cmdbufdesc.hostptr);
310 unsigned int *p = (unsigned int *)(ptr + rb_offset(index));
311
312 *p++ = Z180_STREAM_PACKET;
313 *p++ = (Z180_MARKER_CMD | 5);
314 *p++ = ADDR_VGV3_LAST << 24;
315 *p++ = ADDR_VGV3_LAST << 24;
316 *p++ = ADDR_VGV3_LAST << 24;
317 *p++ = Z180_STREAM_PACKET;
318 *p++ = 5;
319 *p++ = ADDR_VGV3_LAST << 24;
320 *p++ = ADDR_VGV3_LAST << 24;
321 *p++ = ADDR_VGV3_LAST << 24;
322}
323
324static void addcmd(struct z180_ringbuffer *rb, unsigned int index,
325 unsigned int cmd, unsigned int nextcnt)
326{
327 char * ptr = (char *)(rb->cmdbufdesc.hostptr);
328 unsigned int *p = (unsigned int *)(ptr + (rb_offset(index)
329 + (Z180_MARKER_SIZE * sizeof(unsigned int))));
330
331 *p++ = Z180_STREAM_PACKET_CALL;
332 *p++ = cmd;
333 *p++ = Z180_CALL_CMD | nextcnt;
334 *p++ = ADDR_VGV3_LAST << 24;
335 *p++ = ADDR_VGV3_LAST << 24;
336}
337
338static void z180_cmdstream_start(struct kgsl_device *device)
339{
340 struct z180_device *z180_dev = Z180_DEVICE(device);
341 unsigned int cmd = VGV3_NEXTCMD_JUMP << VGV3_NEXTCMD_NEXTCMD_FSHIFT;
342
343 z180_dev->timestamp = 0;
344 z180_dev->current_timestamp = 0;
345
346 addmarker(&z180_dev->ringbuffer, 0);
347
348 z180_cmdwindow_write(device, ADDR_VGV3_MODE, 4);
349
350 z180_cmdwindow_write(device, ADDR_VGV3_NEXTADDR,
351 z180_dev->ringbuffer.cmdbufdesc.gpuaddr);
352
353 z180_cmdwindow_write(device, ADDR_VGV3_NEXTCMD, cmd | 5);
354
355 z180_cmdwindow_write(device, ADDR_VGV3_WRITEADDR,
356 device->memstore.gpuaddr);
357
358 cmd = (int)(((1) & VGV3_CONTROL_MARKADD_FMASK)
359 << VGV3_CONTROL_MARKADD_FSHIFT);
360
361 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, cmd);
362
363 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, 0);
364}
365
366static int room_in_rb(struct z180_device *device)
367{
368 int ts_diff;
369
370 ts_diff = device->current_timestamp - device->timestamp;
371
372 return ts_diff < Z180_PACKET_COUNT;
373}
374
375static int z180_idle(struct kgsl_device *device, unsigned int timeout)
376{
377 int status = 0;
378 struct z180_device *z180_dev = Z180_DEVICE(device);
379
380 if (z180_dev->current_timestamp > z180_dev->timestamp)
381 status = z180_wait(device, z180_dev->current_timestamp,
382 timeout);
383
384 if (status)
385 KGSL_DRV_ERR(device, "z180_waittimestamp() timed out\n");
386
387 return status;
388}
389
390static void z180_setstate(struct kgsl_device *device, uint32_t flags)
391{
392 kgsl_default_setstate(device, flags);
393}
394
395int
396z180_cmdstream_issueibcmds(struct kgsl_device_private *dev_priv,
397 struct kgsl_context *context,
398 struct kgsl_ibdesc *ibdesc,
399 unsigned int numibs,
400 uint32_t *timestamp,
401 unsigned int ctrl)
402{
403 unsigned int result = 0;
404 unsigned int ofs = PACKETSIZE_STATESTREAM * sizeof(unsigned int);
405 unsigned int cnt = 5;
406 unsigned int nextaddr = 0;
407 unsigned int index = 0;
408 unsigned int nextindex;
409 unsigned int nextcnt = Z180_STREAM_END_CMD | 5;
410 struct kgsl_memdesc tmp = {0};
411 unsigned int cmd;
412 struct kgsl_device *device = dev_priv->device;
413 struct kgsl_pagetable *pagetable = dev_priv->process_priv->pagetable;
414 struct z180_device *z180_dev = Z180_DEVICE(device);
415 unsigned int sizedwords;
416
417 if (device->state & KGSL_STATE_HUNG) {
418 return -EINVAL;
419 goto error;
420 }
421 if (numibs != 1) {
422 KGSL_DRV_ERR(device, "Invalid number of ibs: %d\n", numibs);
423 result = -EINVAL;
424 goto error;
425 }
426 cmd = ibdesc[0].gpuaddr;
427 sizedwords = ibdesc[0].sizedwords;
428
429 tmp.hostptr = (void *)*timestamp;
430
431 KGSL_CMD_INFO(device, "ctxt %d ibaddr 0x%08x sizedwords %d\n",
432 context->id, cmd, sizedwords);
433 /* context switch */
434 if ((context->id != (int)z180_dev->ringbuffer.prevctx) ||
435 (ctrl & KGSL_CONTEXT_CTX_SWITCH)) {
436 KGSL_CMD_INFO(device, "context switch %d -> %d\n",
437 context->id, z180_dev->ringbuffer.prevctx);
438 kgsl_mmu_setstate(device, pagetable);
439 cnt = PACKETSIZE_STATESTREAM;
440 ofs = 0;
441 }
442 z180_setstate(device, kgsl_pt_get_flags(device->mmu.hwpagetable,
443 device->id));
444
445 result = wait_event_interruptible_timeout(device->wait_queue,
446 room_in_rb(z180_dev),
447 msecs_to_jiffies(KGSL_TIMEOUT_DEFAULT));
448 if (result < 0) {
449 KGSL_CMD_ERR(device, "wait_event_interruptible_timeout "
450 "failed: %d\n", result);
451 goto error;
452 }
453 result = 0;
454
455 index = z180_dev->current_timestamp % Z180_PACKET_COUNT;
456 z180_dev->current_timestamp++;
457 nextindex = z180_dev->current_timestamp % Z180_PACKET_COUNT;
458 *timestamp = z180_dev->current_timestamp;
459
460 z180_dev->ringbuffer.prevctx = context->id;
461
462 addcmd(&z180_dev->ringbuffer, index, cmd + ofs, cnt);
463
464 /* Make sure the next ringbuffer entry has a marker */
465 addmarker(&z180_dev->ringbuffer, nextindex);
466
467 nextaddr = z180_dev->ringbuffer.cmdbufdesc.gpuaddr
468 + rb_offset(nextindex);
469
470 tmp.hostptr = (void *)(tmp.hostptr +
471 (sizedwords * sizeof(unsigned int)));
472 tmp.size = 12;
473
474 kgsl_sharedmem_writel(&tmp, 4, nextaddr);
475 kgsl_sharedmem_writel(&tmp, 8, nextcnt);
476
477 /* sync memory before activating the hardware for the new command*/
478 mb();
479
480 cmd = (int)(((2) & VGV3_CONTROL_MARKADD_FMASK)
481 << VGV3_CONTROL_MARKADD_FSHIFT);
482
483 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, cmd);
484 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, 0);
485error:
486 return result;
487}
488
489static int z180_ringbuffer_init(struct kgsl_device *device)
490{
491 struct z180_device *z180_dev = Z180_DEVICE(device);
492 memset(&z180_dev->ringbuffer, 0, sizeof(struct z180_ringbuffer));
493 z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT;
494 return kgsl_allocate_contiguous(&z180_dev->ringbuffer.cmdbufdesc,
495 Z180_RB_SIZE);
496}
497
498static void z180_ringbuffer_close(struct kgsl_device *device)
499{
500 struct z180_device *z180_dev = Z180_DEVICE(device);
501 kgsl_sharedmem_free(&z180_dev->ringbuffer.cmdbufdesc);
502 memset(&z180_dev->ringbuffer, 0, sizeof(struct z180_ringbuffer));
503}
504
505static int __devinit z180_probe(struct platform_device *pdev)
506{
507 int status = -EINVAL;
508 struct kgsl_device *device = NULL;
509 struct z180_device *z180_dev;
510
511 device = (struct kgsl_device *)pdev->id_entry->driver_data;
512 device->parentdev = &pdev->dev;
513
514 z180_dev = Z180_DEVICE(device);
515 spin_lock_init(&z180_dev->cmdwin_lock);
516
517 status = z180_ringbuffer_init(device);
518 if (status != 0)
519 goto error;
520
521 status = kgsl_device_platform_probe(device, z180_isr);
522 if (status)
523 goto error_close_ringbuffer;
524
525 return status;
526
527error_close_ringbuffer:
528 z180_ringbuffer_close(device);
529error:
530 device->parentdev = NULL;
531 return status;
532}
533
534static int __devexit z180_remove(struct platform_device *pdev)
535{
536 struct kgsl_device *device = NULL;
537
538 device = (struct kgsl_device *)pdev->id_entry->driver_data;
539
540 kgsl_device_platform_remove(device);
541
542 z180_ringbuffer_close(device);
543
544 return 0;
545}
546
547static int z180_start(struct kgsl_device *device, unsigned int init_ram)
548{
549 int status = 0;
550
551 device->state = KGSL_STATE_INIT;
552 device->requested_state = KGSL_STATE_NONE;
553 KGSL_PWR_WARN(device, "state -> INIT, device %d\n", device->id);
554
555 kgsl_pwrctrl_enable(device);
556
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557 /* Set interrupts to 0 to ensure a good state */
558 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0x0);
559
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600560 kgsl_mh_start(device);
561
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562 status = kgsl_mmu_start(device);
563 if (status)
564 goto error_clk_off;
565
566 z180_cmdstream_start(device);
567
568 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
569 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_IRQ_ON);
570 return 0;
571
572error_clk_off:
573 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0);
574 kgsl_pwrctrl_disable(device);
575 return status;
576}
577
578static int z180_stop(struct kgsl_device *device)
579{
580 z180_idle(device, KGSL_TIMEOUT_DEFAULT);
581
Jeremy Gebben1757a852011-07-11 16:04:38 -0600582 del_timer_sync(&device->idle_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583
584 kgsl_mmu_stop(device);
585
586 /* Disable the clocks before the power rail. */
587 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
588
589 kgsl_pwrctrl_disable(device);
590
591 return 0;
592}
593
594static int z180_getproperty(struct kgsl_device *device,
595 enum kgsl_property_type type,
596 void *value,
597 unsigned int sizebytes)
598{
599 int status = -EINVAL;
600
601 switch (type) {
602 case KGSL_PROP_DEVICE_INFO:
603 {
604 struct kgsl_devinfo devinfo;
605
606 if (sizebytes != sizeof(devinfo)) {
607 status = -EINVAL;
608 break;
609 }
610
611 memset(&devinfo, 0, sizeof(devinfo));
612 devinfo.device_id = device->id+1;
613 devinfo.chip_id = 0;
614 devinfo.mmu_enabled = kgsl_mmu_enabled();
615
616 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
617 0) {
618 status = -EFAULT;
619 break;
620 }
621 status = 0;
622 }
623 break;
624 case KGSL_PROP_MMU_ENABLE:
625 {
626#ifdef CONFIG_MSM_KGSL_MMU
627 int mmuProp = 1;
628#else
629 int mmuProp = 0;
630#endif
631 if (sizebytes != sizeof(int)) {
632 status = -EINVAL;
633 break;
634 }
635 if (copy_to_user(value, &mmuProp, sizeof(mmuProp))) {
636 status = -EFAULT;
637 break;
638 }
639 status = 0;
640 }
641 break;
642
643 default:
644 KGSL_DRV_ERR(device, "invalid property: %d\n", type);
645 status = -EINVAL;
646 }
647 return status;
648}
649
650static unsigned int z180_isidle(struct kgsl_device *device)
651{
652 int status = false;
653 struct z180_device *z180_dev = Z180_DEVICE(device);
654
655 int timestamp = z180_dev->timestamp;
656
657 if (timestamp == z180_dev->current_timestamp)
658 status = true;
659
660 return status;
661}
662
663static int z180_suspend_context(struct kgsl_device *device)
664{
665 struct z180_device *z180_dev = Z180_DEVICE(device);
666
667 z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT;
668
669 return 0;
670}
671
672/* Not all Z180 registers are directly accessible.
673 * The _z180_(read|write)_simple functions below handle the ones that are.
674 */
675static void _z180_regread_simple(struct kgsl_device *device,
676 unsigned int offsetwords,
677 unsigned int *value)
678{
679 unsigned int *reg;
680
681 BUG_ON(offsetwords * sizeof(uint32_t) >= device->regspace.sizebytes);
682
683 reg = (unsigned int *)(device->regspace.mmio_virt_base
684 + (offsetwords << 2));
685
686 /*ensure this read finishes before the next one.
687 * i.e. act like normal readl() */
688 *value = __raw_readl(reg);
689 rmb();
690
691}
692
693static void _z180_regwrite_simple(struct kgsl_device *device,
694 unsigned int offsetwords,
695 unsigned int value)
696{
697 unsigned int *reg;
698
699 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
700
701 reg = (unsigned int *)(device->regspace.mmio_virt_base
702 + (offsetwords << 2));
703 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
704 /*ensure previous writes post before this one,
705 * i.e. act like normal writel() */
706 wmb();
707 __raw_writel(value, reg);
708}
709
710
711/* The MH registers must be accessed through via a 2 step write, (read|write)
712 * process. These registers may be accessed from interrupt context during
713 * the handling of MH or MMU error interrupts. Therefore a spin lock is used
714 * to ensure that the 2 step sequence is not interrupted.
715 */
716static void _z180_regread_mmu(struct kgsl_device *device,
717 unsigned int offsetwords,
718 unsigned int *value)
719{
720 struct z180_device *z180_dev = Z180_DEVICE(device);
721 unsigned long flags;
722
723 spin_lock_irqsave(&z180_dev->cmdwin_lock, flags);
724 _z180_regwrite_simple(device, (ADDR_VGC_MH_READ_ADDR >> 2),
725 offsetwords);
726 _z180_regread_simple(device, (ADDR_VGC_MH_DATA_ADDR >> 2), value);
727 spin_unlock_irqrestore(&z180_dev->cmdwin_lock, flags);
728}
729
730
731static void _z180_regwrite_mmu(struct kgsl_device *device,
732 unsigned int offsetwords,
733 unsigned int value)
734{
735 struct z180_device *z180_dev = Z180_DEVICE(device);
736 unsigned int cmdwinaddr;
737 unsigned long flags;
738
739 cmdwinaddr = ((Z180_CMDWINDOW_MMU << Z180_CMDWINDOW_TARGET_SHIFT) &
740 Z180_CMDWINDOW_TARGET_MASK);
741 cmdwinaddr |= ((offsetwords << Z180_CMDWINDOW_ADDR_SHIFT) &
742 Z180_CMDWINDOW_ADDR_MASK);
743
744 spin_lock_irqsave(&z180_dev->cmdwin_lock, flags);
745 _z180_regwrite_simple(device, ADDR_VGC_MMUCOMMANDSTREAM >> 2,
746 cmdwinaddr);
747 _z180_regwrite_simple(device, ADDR_VGC_MMUCOMMANDSTREAM >> 2, value);
748 spin_unlock_irqrestore(&z180_dev->cmdwin_lock, flags);
749}
750
751/* the rest of the code doesn't want to think about if it is writing mmu
752 * registers or normal registers so handle it here
753 */
754static void z180_regread(struct kgsl_device *device,
755 unsigned int offsetwords,
756 unsigned int *value)
757{
758 if (!in_interrupt())
759 kgsl_pre_hwaccess(device);
760
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600761 if ((offsetwords >= MH_ARBITER_CONFIG &&
762 offsetwords <= MH_AXI_HALT_CONTROL) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763 (offsetwords >= MH_MMU_CONFIG &&
764 offsetwords <= MH_MMU_MPU_END)) {
765 _z180_regread_mmu(device, offsetwords, value);
766 } else {
767 _z180_regread_simple(device, offsetwords, value);
768 }
769}
770
771static void z180_regwrite(struct kgsl_device *device,
772 unsigned int offsetwords,
773 unsigned int value)
774{
775 if (!in_interrupt())
776 kgsl_pre_hwaccess(device);
777
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600778 if ((offsetwords >= MH_ARBITER_CONFIG &&
779 offsetwords <= MH_CLNT_INTF_CTRL_CONFIG2) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780 (offsetwords >= MH_MMU_CONFIG &&
781 offsetwords <= MH_MMU_MPU_END)) {
782 _z180_regwrite_mmu(device, offsetwords, value);
783 } else {
784 _z180_regwrite_simple(device, offsetwords, value);
785 }
786}
787
788static void z180_cmdwindow_write(struct kgsl_device *device,
789 unsigned int addr, unsigned int data)
790{
791 unsigned int cmdwinaddr;
792
793 cmdwinaddr = ((Z180_CMDWINDOW_2D << Z180_CMDWINDOW_TARGET_SHIFT) &
794 Z180_CMDWINDOW_TARGET_MASK);
795 cmdwinaddr |= ((addr << Z180_CMDWINDOW_ADDR_SHIFT) &
796 Z180_CMDWINDOW_ADDR_MASK);
797
798 z180_regwrite(device, ADDR_VGC_COMMANDSTREAM >> 2, cmdwinaddr);
799 z180_regwrite(device, ADDR_VGC_COMMANDSTREAM >> 2, data);
800}
801
802static unsigned int z180_readtimestamp(struct kgsl_device *device,
803 enum kgsl_timestamp_type type)
804{
805 struct z180_device *z180_dev = Z180_DEVICE(device);
806 /* get current EOP timestamp */
807 return z180_dev->timestamp;
808}
809
810static int z180_waittimestamp(struct kgsl_device *device,
811 unsigned int timestamp,
812 unsigned int msecs)
813{
814 int status = -EINVAL;
815 mutex_unlock(&device->mutex);
816 status = z180_wait(device, timestamp, msecs);
817 mutex_lock(&device->mutex);
818
819 return status;
820}
821
822static int z180_wait(struct kgsl_device *device,
823 unsigned int timestamp,
824 unsigned int msecs)
825{
826 int status = -EINVAL;
827 long timeout = 0;
828
829 timeout = wait_io_event_interruptible_timeout(
830 device->wait_queue,
831 kgsl_check_timestamp(device, timestamp),
832 msecs_to_jiffies(msecs));
833
834 if (timeout > 0)
835 status = 0;
836 else if (timeout == 0) {
837 status = -ETIMEDOUT;
838 device->state = KGSL_STATE_HUNG;
839 KGSL_PWR_WARN(device, "state -> HUNG, device %d\n", device->id);
840 } else
841 status = timeout;
842
843 return status;
844}
845
846static void
847z180_drawctxt_destroy(struct kgsl_device *device,
848 struct kgsl_context *context)
849{
850 struct z180_device *z180_dev = Z180_DEVICE(device);
851
852 z180_idle(device, KGSL_TIMEOUT_DEFAULT);
853
854 if (z180_dev->ringbuffer.prevctx == context->id) {
855 z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT;
856 device->mmu.hwpagetable = device->mmu.defaultpagetable;
857 kgsl_setstate(device, KGSL_MMUFLAGS_PTUPDATE);
858 }
859}
860
861static void z180_power_stats(struct kgsl_device *device,
862 struct kgsl_power_stats *stats)
863{
864 stats->total_time = 0;
865 stats->busy_time = 0;
866}
867
868static void z180_irqctrl(struct kgsl_device *device, int state)
869{
870 /* Control interrupts for Z180 and the Z180 MMU */
871
872 if (state) {
873 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 3);
874 z180_regwrite(device, MH_INTERRUPT_MASK, KGSL_MMU_INT_MASK);
875 } else {
876 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0);
877 z180_regwrite(device, MH_INTERRUPT_MASK, 0);
878 }
879}
880
881static const struct kgsl_functable z180_functable = {
882 /* Mandatory functions */
883 .regread = z180_regread,
884 .regwrite = z180_regwrite,
885 .idle = z180_idle,
886 .isidle = z180_isidle,
887 .suspend_context = z180_suspend_context,
888 .start = z180_start,
889 .stop = z180_stop,
890 .getproperty = z180_getproperty,
891 .waittimestamp = z180_waittimestamp,
892 .readtimestamp = z180_readtimestamp,
893 .issueibcmds = z180_cmdstream_issueibcmds,
894 .setup_pt = z180_setup_pt,
895 .cleanup_pt = z180_cleanup_pt,
896 .power_stats = z180_power_stats,
897 .irqctrl = z180_irqctrl,
898 /* Optional functions */
899 .setstate = z180_setstate,
900 .drawctxt_create = NULL,
901 .drawctxt_destroy = z180_drawctxt_destroy,
902 .ioctl = NULL,
903};
904
905static struct platform_device_id z180_id_table[] = {
906 { DEVICE_2D0_NAME, (kernel_ulong_t)&device_2d0.dev, },
907 { DEVICE_2D1_NAME, (kernel_ulong_t)&device_2d1.dev, },
908 { },
909};
910MODULE_DEVICE_TABLE(platform, z180_id_table);
911
912static struct platform_driver z180_platform_driver = {
913 .probe = z180_probe,
914 .remove = __devexit_p(z180_remove),
915 .suspend = kgsl_suspend_driver,
916 .resume = kgsl_resume_driver,
917 .id_table = z180_id_table,
918 .driver = {
919 .owner = THIS_MODULE,
920 .name = DEVICE_2D_NAME,
921 .pm = &kgsl_pm_ops,
922 }
923};
924
925static int __init kgsl_2d_init(void)
926{
927 return platform_driver_register(&z180_platform_driver);
928}
929
930static void __exit kgsl_2d_exit(void)
931{
932 platform_driver_unregister(&z180_platform_driver);
933}
934
935module_init(kgsl_2d_init);
936module_exit(kgsl_2d_exit);
937
938MODULE_DESCRIPTION("2D Graphics driver");
939MODULE_VERSION("1.2");
940MODULE_LICENSE("GPL v2");
941MODULE_ALIAS("platform:kgsl_2d");