blob: de7d1befdcfbff5556914c3fcbeab3b426c6015f [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"
Norman Geed7402ff2011-10-28 08:51:11 -060021#include "z180_trace.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022
23#define DRIVER_VERSION_MAJOR 3
24#define DRIVER_VERSION_MINOR 1
25
26#define Z180_DEVICE(device) \
27 KGSL_CONTAINER_OF(device, struct z180_device, dev)
28
29#define GSL_VGC_INT_MASK \
30 (REG_VGC_IRQSTATUS__MH_MASK | \
31 REG_VGC_IRQSTATUS__G2D_MASK | \
32 REG_VGC_IRQSTATUS__FIFO_MASK)
33
34#define VGV3_NEXTCMD_JUMP 0x01
35
36#define VGV3_NEXTCMD_NEXTCMD_FSHIFT 12
37#define VGV3_NEXTCMD_NEXTCMD_FMASK 0x7
38
39#define VGV3_CONTROL_MARKADD_FSHIFT 0
40#define VGV3_CONTROL_MARKADD_FMASK 0xfff
41
42#define Z180_PACKET_SIZE 15
43#define Z180_MARKER_SIZE 10
44#define Z180_CALL_CMD 0x1000
45#define Z180_MARKER_CMD 0x8000
46#define Z180_STREAM_END_CMD 0x9000
47#define Z180_STREAM_PACKET 0x7C000176
48#define Z180_STREAM_PACKET_CALL 0x7C000275
49#define Z180_PACKET_COUNT 8
50#define Z180_RB_SIZE (Z180_PACKET_SIZE*Z180_PACKET_COUNT \
51 *sizeof(uint32_t))
52
53#define NUMTEXUNITS 4
54#define TEXUNITREGCOUNT 25
55#define VG_REGCOUNT 0x39
56
57#define PACKETSIZE_BEGIN 3
58#define PACKETSIZE_G2DCOLOR 2
59#define PACKETSIZE_TEXUNIT (TEXUNITREGCOUNT * 2)
60#define PACKETSIZE_REG (VG_REGCOUNT * 2)
61#define PACKETSIZE_STATE (PACKETSIZE_TEXUNIT * NUMTEXUNITS + \
62 PACKETSIZE_REG + PACKETSIZE_BEGIN + \
63 PACKETSIZE_G2DCOLOR)
64#define PACKETSIZE_STATESTREAM (ALIGN((PACKETSIZE_STATE * \
65 sizeof(unsigned int)), 32) / \
66 sizeof(unsigned int))
67
68#define Z180_INVALID_CONTEXT UINT_MAX
69
70/* z180 MH arbiter config*/
71#define Z180_CFG_MHARB \
72 (0x10 \
73 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
74 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
75 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
76 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
77 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
78 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
79 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
80 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
81 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
82 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
83 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
84 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
85 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
86 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
87
88#define Z180_TIMESTAMP_EPSILON 20000
89#define Z180_IDLE_COUNT_MAX 1000000
90
91enum z180_cmdwindow_type {
92 Z180_CMDWINDOW_2D = 0x00000000,
93 Z180_CMDWINDOW_MMU = 0x00000002,
94};
95
96#define Z180_CMDWINDOW_TARGET_MASK 0x000000FF
97#define Z180_CMDWINDOW_ADDR_MASK 0x00FFFF00
98#define Z180_CMDWINDOW_TARGET_SHIFT 0
99#define Z180_CMDWINDOW_ADDR_SHIFT 8
100
101static int z180_start(struct kgsl_device *device, unsigned int init_ram);
102static int z180_stop(struct kgsl_device *device);
103static int z180_wait(struct kgsl_device *device,
104 unsigned int timestamp,
105 unsigned int msecs);
106static void z180_regread(struct kgsl_device *device,
107 unsigned int offsetwords,
108 unsigned int *value);
109static void z180_regwrite(struct kgsl_device *device,
110 unsigned int offsetwords,
111 unsigned int value);
112static void z180_cmdwindow_write(struct kgsl_device *device,
113 unsigned int addr,
114 unsigned int data);
115
116#define Z180_MMU_CONFIG \
117 (0x01 \
118 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
119 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
120 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
121 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
122 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
123 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
124 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
125 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
126 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
127 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
128 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
129
130static const struct kgsl_functable z180_functable;
131
132static struct z180_device device_2d0 = {
133 .dev = {
134 .name = DEVICE_2D0_NAME,
135 .id = KGSL_DEVICE_2D0,
136 .ver_major = DRIVER_VERSION_MAJOR,
137 .ver_minor = DRIVER_VERSION_MINOR,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600138 .mh = {
139 .mharb = Z180_CFG_MHARB,
140 .mh_intf_cfg1 = 0x00032f07,
141 .mh_intf_cfg2 = 0x004b274f,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142 /* turn off memory protection unit by setting
143 acceptable physical address range to include
144 all pages. */
145 .mpu_base = 0x00000000,
146 .mpu_range = 0xFFFFF000,
147 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600148 .mmu = {
149 .config = Z180_MMU_CONFIG,
150 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700151 .pwrctrl = {
152 .regulator_name = "fs_gfx2d0",
153 .irq_name = KGSL_2D0_IRQ,
154 },
155 .mutex = __MUTEX_INITIALIZER(device_2d0.dev.mutex),
156 .state = KGSL_STATE_INIT,
157 .active_cnt = 0,
158 .iomemname = KGSL_2D0_REG_MEMORY,
159 .ftbl = &z180_functable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160#ifdef CONFIG_HAS_EARLYSUSPEND
Jordan Crouse9f739212011-07-28 08:37:57 -0600161 .display_off = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700162 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
163 .suspend = kgsl_early_suspend_driver,
164 .resume = kgsl_late_resume_driver,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700165 },
Jordan Crouse9f739212011-07-28 08:37:57 -0600166#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167 },
168};
169
170static struct z180_device device_2d1 = {
171 .dev = {
172 .name = DEVICE_2D1_NAME,
173 .id = KGSL_DEVICE_2D1,
174 .ver_major = DRIVER_VERSION_MAJOR,
175 .ver_minor = DRIVER_VERSION_MINOR,
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600176 .mh = {
177 .mharb = Z180_CFG_MHARB,
178 .mh_intf_cfg1 = 0x00032f07,
179 .mh_intf_cfg2 = 0x004b274f,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180 /* turn off memory protection unit by setting
181 acceptable physical address range to include
182 all pages. */
183 .mpu_base = 0x00000000,
184 .mpu_range = 0xFFFFF000,
185 },
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600186 .mmu = {
187 .config = Z180_MMU_CONFIG,
188 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189 .pwrctrl = {
190 .regulator_name = "fs_gfx2d1",
191 .irq_name = KGSL_2D1_IRQ,
192 },
193 .mutex = __MUTEX_INITIALIZER(device_2d1.dev.mutex),
194 .state = KGSL_STATE_INIT,
195 .active_cnt = 0,
196 .iomemname = KGSL_2D1_REG_MEMORY,
197 .ftbl = &z180_functable,
198 .display_off = {
199#ifdef CONFIG_HAS_EARLYSUSPEND
200 .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
201 .suspend = kgsl_early_suspend_driver,
202 .resume = kgsl_late_resume_driver,
203#endif
204 },
205 },
206};
207
208static irqreturn_t z180_isr(int irq, void *data)
209{
210 irqreturn_t result = IRQ_NONE;
211 unsigned int status;
212 struct kgsl_device *device = (struct kgsl_device *) data;
213 struct z180_device *z180_dev = Z180_DEVICE(device);
214
215 z180_regread(device, ADDR_VGC_IRQSTATUS >> 2, &status);
216
Norman Geed7402ff2011-10-28 08:51:11 -0600217 trace_kgsl_z180_irq_status(device, status);
218
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219 if (status & GSL_VGC_INT_MASK) {
220 z180_regwrite(device,
221 ADDR_VGC_IRQSTATUS >> 2, status & GSL_VGC_INT_MASK);
222
223 result = IRQ_HANDLED;
224
225 if (status & REG_VGC_IRQSTATUS__FIFO_MASK)
226 KGSL_DRV_ERR(device, "z180 fifo interrupt\n");
227 if (status & REG_VGC_IRQSTATUS__MH_MASK)
228 kgsl_mh_intrcallback(device);
229 if (status & REG_VGC_IRQSTATUS__G2D_MASK) {
230 int count;
231
232 z180_regread(device,
233 ADDR_VGC_IRQ_ACTIVE_CNT >> 2,
234 &count);
235
236 count >>= 8;
237 count &= 255;
238 z180_dev->timestamp += count;
239
Jordan Crouse1bf80aa2011-10-12 16:57:47 -0600240 queue_work(device->work_queue, &device->ts_expired_ws);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700241 wake_up_interruptible(&device->wait_queue);
242
243 atomic_notifier_call_chain(
244 &(device->ts_notifier_list),
245 device->id, NULL);
246 }
247 }
248
249 if ((device->pwrctrl.nap_allowed == true) &&
250 (device->requested_state == KGSL_STATE_NONE)) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700251 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 queue_work(device->work_queue, &device->idle_check_ws);
253 }
254 mod_timer(&device->idle_timer,
255 jiffies + device->pwrctrl.interval_timeout);
256
257 return result;
258}
259
Jordan Crouse9f739212011-07-28 08:37:57 -0600260static void z180_cleanup_pt(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261 struct kgsl_pagetable *pagetable)
262{
263 struct z180_device *z180_dev = Z180_DEVICE(device);
264
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600265 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266
267 kgsl_mmu_unmap(pagetable, &device->memstore);
268
269 kgsl_mmu_unmap(pagetable, &z180_dev->ringbuffer.cmdbufdesc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700270}
271
272static int z180_setup_pt(struct kgsl_device *device,
273 struct kgsl_pagetable *pagetable)
274{
275 int result = 0;
276 struct z180_device *z180_dev = Z180_DEVICE(device);
277
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600278 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
280
281 if (result)
282 goto error;
283
284 result = kgsl_mmu_map_global(pagetable, &device->memstore,
285 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
286 if (result)
287 goto error_unmap_dummy;
288
289 result = kgsl_mmu_map_global(pagetable,
290 &z180_dev->ringbuffer.cmdbufdesc,
291 GSL_PT_PAGE_RV);
292 if (result)
293 goto error_unmap_memstore;
294 return result;
295
296error_unmap_dummy:
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600297 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298
299error_unmap_memstore:
300 kgsl_mmu_unmap(pagetable, &device->memstore);
301
302error:
303 return result;
304}
305
306static inline unsigned int rb_offset(unsigned int index)
307{
308 return index*sizeof(unsigned int)*(Z180_PACKET_SIZE);
309}
310
311static void addmarker(struct z180_ringbuffer *rb, unsigned int index)
312{
313 char *ptr = (char *)(rb->cmdbufdesc.hostptr);
314 unsigned int *p = (unsigned int *)(ptr + rb_offset(index));
315
316 *p++ = Z180_STREAM_PACKET;
317 *p++ = (Z180_MARKER_CMD | 5);
318 *p++ = ADDR_VGV3_LAST << 24;
319 *p++ = ADDR_VGV3_LAST << 24;
320 *p++ = ADDR_VGV3_LAST << 24;
321 *p++ = Z180_STREAM_PACKET;
322 *p++ = 5;
323 *p++ = ADDR_VGV3_LAST << 24;
324 *p++ = ADDR_VGV3_LAST << 24;
325 *p++ = ADDR_VGV3_LAST << 24;
326}
327
328static void addcmd(struct z180_ringbuffer *rb, unsigned int index,
329 unsigned int cmd, unsigned int nextcnt)
330{
331 char * ptr = (char *)(rb->cmdbufdesc.hostptr);
332 unsigned int *p = (unsigned int *)(ptr + (rb_offset(index)
333 + (Z180_MARKER_SIZE * sizeof(unsigned int))));
334
335 *p++ = Z180_STREAM_PACKET_CALL;
336 *p++ = cmd;
337 *p++ = Z180_CALL_CMD | nextcnt;
338 *p++ = ADDR_VGV3_LAST << 24;
339 *p++ = ADDR_VGV3_LAST << 24;
340}
341
342static void z180_cmdstream_start(struct kgsl_device *device)
343{
344 struct z180_device *z180_dev = Z180_DEVICE(device);
345 unsigned int cmd = VGV3_NEXTCMD_JUMP << VGV3_NEXTCMD_NEXTCMD_FSHIFT;
346
347 z180_dev->timestamp = 0;
348 z180_dev->current_timestamp = 0;
349
350 addmarker(&z180_dev->ringbuffer, 0);
351
352 z180_cmdwindow_write(device, ADDR_VGV3_MODE, 4);
353
354 z180_cmdwindow_write(device, ADDR_VGV3_NEXTADDR,
355 z180_dev->ringbuffer.cmdbufdesc.gpuaddr);
356
357 z180_cmdwindow_write(device, ADDR_VGV3_NEXTCMD, cmd | 5);
358
359 z180_cmdwindow_write(device, ADDR_VGV3_WRITEADDR,
360 device->memstore.gpuaddr);
361
362 cmd = (int)(((1) & VGV3_CONTROL_MARKADD_FMASK)
363 << VGV3_CONTROL_MARKADD_FSHIFT);
364
365 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, cmd);
366
367 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, 0);
368}
369
370static int room_in_rb(struct z180_device *device)
371{
372 int ts_diff;
373
374 ts_diff = device->current_timestamp - device->timestamp;
375
376 return ts_diff < Z180_PACKET_COUNT;
377}
378
379static int z180_idle(struct kgsl_device *device, unsigned int timeout)
380{
381 int status = 0;
382 struct z180_device *z180_dev = Z180_DEVICE(device);
383
Jordan Crousee6239dd2011-11-17 13:39:21 -0700384 if (timestamp_cmp(z180_dev->current_timestamp,
385 z180_dev->timestamp) > 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700386 status = z180_wait(device, z180_dev->current_timestamp,
387 timeout);
388
389 if (status)
390 KGSL_DRV_ERR(device, "z180_waittimestamp() timed out\n");
391
392 return status;
393}
394
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700395int
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{
Tarun Karra9b92ccd2011-08-19 10:59:57 -0700403 long result = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700404 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) {
Carter Cooperb90880d2011-10-26 14:38:02 -0600418 result = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700419 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 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600442 kgsl_setstate(device, kgsl_mmu_pt_get_flags(device->mmu.hwpagetable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443 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 "
Tarun Karra9b92ccd2011-08-19 10:59:57 -0700450 "failed: %ld\n", result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700451 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);
Lucille Sylvester808eca22011-11-03 10:26:29 -0700463 kgsl_pwrscale_busy(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464
465 /* Make sure the next ringbuffer entry has a marker */
466 addmarker(&z180_dev->ringbuffer, nextindex);
467
468 nextaddr = z180_dev->ringbuffer.cmdbufdesc.gpuaddr
469 + rb_offset(nextindex);
470
471 tmp.hostptr = (void *)(tmp.hostptr +
472 (sizedwords * sizeof(unsigned int)));
473 tmp.size = 12;
474
475 kgsl_sharedmem_writel(&tmp, 4, nextaddr);
476 kgsl_sharedmem_writel(&tmp, 8, nextcnt);
477
478 /* sync memory before activating the hardware for the new command*/
479 mb();
480
481 cmd = (int)(((2) & VGV3_CONTROL_MARKADD_FMASK)
482 << VGV3_CONTROL_MARKADD_FSHIFT);
483
484 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, cmd);
485 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, 0);
486error:
Tarun Karra9b92ccd2011-08-19 10:59:57 -0700487 return (int)result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700488}
489
490static int z180_ringbuffer_init(struct kgsl_device *device)
491{
492 struct z180_device *z180_dev = Z180_DEVICE(device);
493 memset(&z180_dev->ringbuffer, 0, sizeof(struct z180_ringbuffer));
494 z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT;
495 return kgsl_allocate_contiguous(&z180_dev->ringbuffer.cmdbufdesc,
496 Z180_RB_SIZE);
497}
498
499static void z180_ringbuffer_close(struct kgsl_device *device)
500{
501 struct z180_device *z180_dev = Z180_DEVICE(device);
502 kgsl_sharedmem_free(&z180_dev->ringbuffer.cmdbufdesc);
503 memset(&z180_dev->ringbuffer, 0, sizeof(struct z180_ringbuffer));
504}
505
506static int __devinit z180_probe(struct platform_device *pdev)
507{
508 int status = -EINVAL;
509 struct kgsl_device *device = NULL;
510 struct z180_device *z180_dev;
511
512 device = (struct kgsl_device *)pdev->id_entry->driver_data;
513 device->parentdev = &pdev->dev;
514
515 z180_dev = Z180_DEVICE(device);
516 spin_lock_init(&z180_dev->cmdwin_lock);
517
518 status = z180_ringbuffer_init(device);
519 if (status != 0)
520 goto error;
521
522 status = kgsl_device_platform_probe(device, z180_isr);
523 if (status)
524 goto error_close_ringbuffer;
525
Lucille Sylvester591ea032011-07-21 16:08:37 -0600526 kgsl_pwrscale_init(device);
Lucille Sylvester808eca22011-11-03 10:26:29 -0700527 kgsl_pwrscale_attach_policy(device, Z180_DEFAULT_PWRSCALE_POLICY);
Lucille Sylvester591ea032011-07-21 16:08:37 -0600528
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529 return status;
530
531error_close_ringbuffer:
532 z180_ringbuffer_close(device);
533error:
534 device->parentdev = NULL;
535 return status;
536}
537
538static int __devexit z180_remove(struct platform_device *pdev)
539{
540 struct kgsl_device *device = NULL;
541
542 device = (struct kgsl_device *)pdev->id_entry->driver_data;
543
Lucille Sylvester591ea032011-07-21 16:08:37 -0600544 kgsl_pwrscale_close(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545 kgsl_device_platform_remove(device);
546
547 z180_ringbuffer_close(device);
548
549 return 0;
550}
551
552static int z180_start(struct kgsl_device *device, unsigned int init_ram)
553{
554 int status = 0;
555
Jeremy Gebben388c2972011-12-16 09:05:07 -0700556 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557
558 kgsl_pwrctrl_enable(device);
559
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560 /* Set interrupts to 0 to ensure a good state */
561 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0x0);
562
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600563 kgsl_mh_start(device);
564
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565 status = kgsl_mmu_start(device);
566 if (status)
567 goto error_clk_off;
568
569 z180_cmdstream_start(device);
570
571 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jeremy Gebbenb46f4152011-10-14 14:27:00 -0600572 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573 return 0;
574
575error_clk_off:
576 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0);
577 kgsl_pwrctrl_disable(device);
578 return status;
579}
580
581static int z180_stop(struct kgsl_device *device)
582{
583 z180_idle(device, KGSL_TIMEOUT_DEFAULT);
584
Jeremy Gebben1757a852011-07-11 16:04:38 -0600585 del_timer_sync(&device->idle_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586
587 kgsl_mmu_stop(device);
588
589 /* Disable the clocks before the power rail. */
590 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
591
592 kgsl_pwrctrl_disable(device);
593
594 return 0;
595}
596
597static int z180_getproperty(struct kgsl_device *device,
598 enum kgsl_property_type type,
599 void *value,
600 unsigned int sizebytes)
601{
602 int status = -EINVAL;
603
604 switch (type) {
605 case KGSL_PROP_DEVICE_INFO:
606 {
607 struct kgsl_devinfo devinfo;
608
609 if (sizebytes != sizeof(devinfo)) {
610 status = -EINVAL;
611 break;
612 }
613
614 memset(&devinfo, 0, sizeof(devinfo));
615 devinfo.device_id = device->id+1;
616 devinfo.chip_id = 0;
617 devinfo.mmu_enabled = kgsl_mmu_enabled();
618
619 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
620 0) {
621 status = -EFAULT;
622 break;
623 }
624 status = 0;
625 }
626 break;
627 case KGSL_PROP_MMU_ENABLE:
628 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600629 int mmu_prop = kgsl_mmu_enabled();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700630 if (sizebytes != sizeof(int)) {
631 status = -EINVAL;
632 break;
633 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600634 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700635 status = -EFAULT;
636 break;
637 }
638 status = 0;
639 }
640 break;
641
642 default:
643 KGSL_DRV_ERR(device, "invalid property: %d\n", type);
644 status = -EINVAL;
645 }
646 return status;
647}
648
649static unsigned int z180_isidle(struct kgsl_device *device)
650{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651 struct z180_device *z180_dev = Z180_DEVICE(device);
652
Jordan Crousee6239dd2011-11-17 13:39:21 -0700653 return (timestamp_cmp(z180_dev->timestamp,
654 z180_dev->current_timestamp) == 0) ? true : false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700655}
656
657static int z180_suspend_context(struct kgsl_device *device)
658{
659 struct z180_device *z180_dev = Z180_DEVICE(device);
660
661 z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT;
662
663 return 0;
664}
665
666/* Not all Z180 registers are directly accessible.
667 * The _z180_(read|write)_simple functions below handle the ones that are.
668 */
669static void _z180_regread_simple(struct kgsl_device *device,
670 unsigned int offsetwords,
671 unsigned int *value)
672{
673 unsigned int *reg;
674
675 BUG_ON(offsetwords * sizeof(uint32_t) >= device->regspace.sizebytes);
676
677 reg = (unsigned int *)(device->regspace.mmio_virt_base
678 + (offsetwords << 2));
679
680 /*ensure this read finishes before the next one.
681 * i.e. act like normal readl() */
682 *value = __raw_readl(reg);
683 rmb();
684
685}
686
687static void _z180_regwrite_simple(struct kgsl_device *device,
688 unsigned int offsetwords,
689 unsigned int value)
690{
691 unsigned int *reg;
692
693 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
694
695 reg = (unsigned int *)(device->regspace.mmio_virt_base
696 + (offsetwords << 2));
697 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
698 /*ensure previous writes post before this one,
699 * i.e. act like normal writel() */
700 wmb();
701 __raw_writel(value, reg);
702}
703
704
705/* The MH registers must be accessed through via a 2 step write, (read|write)
706 * process. These registers may be accessed from interrupt context during
707 * the handling of MH or MMU error interrupts. Therefore a spin lock is used
708 * to ensure that the 2 step sequence is not interrupted.
709 */
710static void _z180_regread_mmu(struct kgsl_device *device,
711 unsigned int offsetwords,
712 unsigned int *value)
713{
714 struct z180_device *z180_dev = Z180_DEVICE(device);
715 unsigned long flags;
716
717 spin_lock_irqsave(&z180_dev->cmdwin_lock, flags);
718 _z180_regwrite_simple(device, (ADDR_VGC_MH_READ_ADDR >> 2),
719 offsetwords);
720 _z180_regread_simple(device, (ADDR_VGC_MH_DATA_ADDR >> 2), value);
721 spin_unlock_irqrestore(&z180_dev->cmdwin_lock, flags);
722}
723
724
725static void _z180_regwrite_mmu(struct kgsl_device *device,
726 unsigned int offsetwords,
727 unsigned int value)
728{
729 struct z180_device *z180_dev = Z180_DEVICE(device);
730 unsigned int cmdwinaddr;
731 unsigned long flags;
732
733 cmdwinaddr = ((Z180_CMDWINDOW_MMU << Z180_CMDWINDOW_TARGET_SHIFT) &
734 Z180_CMDWINDOW_TARGET_MASK);
735 cmdwinaddr |= ((offsetwords << Z180_CMDWINDOW_ADDR_SHIFT) &
736 Z180_CMDWINDOW_ADDR_MASK);
737
738 spin_lock_irqsave(&z180_dev->cmdwin_lock, flags);
739 _z180_regwrite_simple(device, ADDR_VGC_MMUCOMMANDSTREAM >> 2,
740 cmdwinaddr);
741 _z180_regwrite_simple(device, ADDR_VGC_MMUCOMMANDSTREAM >> 2, value);
742 spin_unlock_irqrestore(&z180_dev->cmdwin_lock, flags);
743}
744
745/* the rest of the code doesn't want to think about if it is writing mmu
746 * registers or normal registers so handle it here
747 */
748static void z180_regread(struct kgsl_device *device,
749 unsigned int offsetwords,
750 unsigned int *value)
751{
752 if (!in_interrupt())
753 kgsl_pre_hwaccess(device);
754
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600755 if ((offsetwords >= MH_ARBITER_CONFIG &&
756 offsetwords <= MH_AXI_HALT_CONTROL) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700757 (offsetwords >= MH_MMU_CONFIG &&
758 offsetwords <= MH_MMU_MPU_END)) {
759 _z180_regread_mmu(device, offsetwords, value);
760 } else {
761 _z180_regread_simple(device, offsetwords, value);
762 }
763}
764
765static void z180_regwrite(struct kgsl_device *device,
766 unsigned int offsetwords,
767 unsigned int value)
768{
769 if (!in_interrupt())
770 kgsl_pre_hwaccess(device);
771
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600772 if ((offsetwords >= MH_ARBITER_CONFIG &&
773 offsetwords <= MH_CLNT_INTF_CTRL_CONFIG2) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774 (offsetwords >= MH_MMU_CONFIG &&
775 offsetwords <= MH_MMU_MPU_END)) {
776 _z180_regwrite_mmu(device, offsetwords, value);
777 } else {
778 _z180_regwrite_simple(device, offsetwords, value);
779 }
780}
781
782static void z180_cmdwindow_write(struct kgsl_device *device,
783 unsigned int addr, unsigned int data)
784{
785 unsigned int cmdwinaddr;
786
787 cmdwinaddr = ((Z180_CMDWINDOW_2D << Z180_CMDWINDOW_TARGET_SHIFT) &
788 Z180_CMDWINDOW_TARGET_MASK);
789 cmdwinaddr |= ((addr << Z180_CMDWINDOW_ADDR_SHIFT) &
790 Z180_CMDWINDOW_ADDR_MASK);
791
792 z180_regwrite(device, ADDR_VGC_COMMANDSTREAM >> 2, cmdwinaddr);
793 z180_regwrite(device, ADDR_VGC_COMMANDSTREAM >> 2, data);
794}
795
796static unsigned int z180_readtimestamp(struct kgsl_device *device,
797 enum kgsl_timestamp_type type)
798{
799 struct z180_device *z180_dev = Z180_DEVICE(device);
800 /* get current EOP timestamp */
801 return z180_dev->timestamp;
802}
803
804static int z180_waittimestamp(struct kgsl_device *device,
805 unsigned int timestamp,
806 unsigned int msecs)
807{
808 int status = -EINVAL;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530809
810 /* Don't wait forever, set a max (10 sec) value for now */
811 if (msecs == -1)
812 msecs = 10 * MSEC_PER_SEC;
813
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814 mutex_unlock(&device->mutex);
815 status = z180_wait(device, timestamp, msecs);
816 mutex_lock(&device->mutex);
817
818 return status;
819}
820
821static int z180_wait(struct kgsl_device *device,
822 unsigned int timestamp,
823 unsigned int msecs)
824{
825 int status = -EINVAL;
826 long timeout = 0;
827
828 timeout = wait_io_event_interruptible_timeout(
829 device->wait_queue,
830 kgsl_check_timestamp(device, timestamp),
831 msecs_to_jiffies(msecs));
832
833 if (timeout > 0)
834 status = 0;
835 else if (timeout == 0) {
836 status = -ETIMEDOUT;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700837 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838 } else
839 status = timeout;
840
841 return status;
842}
843
844static void
845z180_drawctxt_destroy(struct kgsl_device *device,
846 struct kgsl_context *context)
847{
848 struct z180_device *z180_dev = Z180_DEVICE(device);
849
850 z180_idle(device, KGSL_TIMEOUT_DEFAULT);
851
852 if (z180_dev->ringbuffer.prevctx == context->id) {
853 z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT;
854 device->mmu.hwpagetable = device->mmu.defaultpagetable;
855 kgsl_setstate(device, KGSL_MMUFLAGS_PTUPDATE);
856 }
857}
858
859static void z180_power_stats(struct kgsl_device *device,
860 struct kgsl_power_stats *stats)
861{
Lucille Sylvester591ea032011-07-21 16:08:37 -0600862 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lucille Sylvester808eca22011-11-03 10:26:29 -0700863 s64 tmp = ktime_to_us(ktime_get());
Lucille Sylvester591ea032011-07-21 16:08:37 -0600864
865 if (pwr->time == 0) {
Lucille Sylvester808eca22011-11-03 10:26:29 -0700866 pwr->time = tmp;
Lucille Sylvester591ea032011-07-21 16:08:37 -0600867 stats->total_time = 0;
868 stats->busy_time = 0;
869 } else {
Lucille Sylvester591ea032011-07-21 16:08:37 -0600870 stats->total_time = tmp - pwr->time;
Lucille Sylvester591ea032011-07-21 16:08:37 -0600871 pwr->time = tmp;
Lucille Sylvester808eca22011-11-03 10:26:29 -0700872 stats->busy_time = tmp - device->on_time;
873 device->on_time = tmp;
Lucille Sylvester591ea032011-07-21 16:08:37 -0600874 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700875}
876
877static void z180_irqctrl(struct kgsl_device *device, int state)
878{
879 /* Control interrupts for Z180 and the Z180 MMU */
880
881 if (state) {
882 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 3);
883 z180_regwrite(device, MH_INTERRUPT_MASK, KGSL_MMU_INT_MASK);
884 } else {
885 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0);
886 z180_regwrite(device, MH_INTERRUPT_MASK, 0);
887 }
888}
889
Jordan Crousea0758f22011-12-07 11:19:22 -0700890static unsigned int z180_gpuid(struct kgsl_device *device)
891{
892 /* Standard KGSL gpuid format:
893 * top word is 0x0002 for 2D or 0x0003 for 3D
894 * Bottom word is core specific identifer
895 */
896
897 return (0x0002 << 16) | 180;
898}
899
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700900static const struct kgsl_functable z180_functable = {
901 /* Mandatory functions */
902 .regread = z180_regread,
903 .regwrite = z180_regwrite,
904 .idle = z180_idle,
905 .isidle = z180_isidle,
906 .suspend_context = z180_suspend_context,
907 .start = z180_start,
908 .stop = z180_stop,
909 .getproperty = z180_getproperty,
910 .waittimestamp = z180_waittimestamp,
911 .readtimestamp = z180_readtimestamp,
912 .issueibcmds = z180_cmdstream_issueibcmds,
913 .setup_pt = z180_setup_pt,
914 .cleanup_pt = z180_cleanup_pt,
915 .power_stats = z180_power_stats,
916 .irqctrl = z180_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -0700917 .gpuid = z180_gpuid,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700918 /* Optional functions */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919 .drawctxt_create = NULL,
920 .drawctxt_destroy = z180_drawctxt_destroy,
921 .ioctl = NULL,
922};
923
924static struct platform_device_id z180_id_table[] = {
925 { DEVICE_2D0_NAME, (kernel_ulong_t)&device_2d0.dev, },
926 { DEVICE_2D1_NAME, (kernel_ulong_t)&device_2d1.dev, },
927 { },
928};
929MODULE_DEVICE_TABLE(platform, z180_id_table);
930
931static struct platform_driver z180_platform_driver = {
932 .probe = z180_probe,
933 .remove = __devexit_p(z180_remove),
934 .suspend = kgsl_suspend_driver,
935 .resume = kgsl_resume_driver,
936 .id_table = z180_id_table,
937 .driver = {
938 .owner = THIS_MODULE,
939 .name = DEVICE_2D_NAME,
940 .pm = &kgsl_pm_ops,
941 }
942};
943
944static int __init kgsl_2d_init(void)
945{
946 return platform_driver_register(&z180_platform_driver);
947}
948
949static void __exit kgsl_2d_exit(void)
950{
951 platform_driver_unregister(&z180_platform_driver);
952}
953
954module_init(kgsl_2d_init);
955module_exit(kgsl_2d_exit);
956
957MODULE_DESCRIPTION("2D Graphics driver");
958MODULE_VERSION("1.2");
959MODULE_LICENSE("GPL v2");
960MODULE_ALIAS("platform:kgsl_2d");