blob: 6c43a7552fde4bce0b011347c23e68643b0cbdb9 [file] [log] [blame]
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -07001/* Copyright (c) 2002,2007-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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 }
Tarun Karra68755762012-01-12 16:07:09 -0800254 mod_timer_pending(&device->idle_timer,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 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
Shubhraprakash Das8cbd2322012-03-04 14:41:39 -0700342static void z180_cmdstream_start(struct kgsl_device *device, int init_ram)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700343{
344 struct z180_device *z180_dev = Z180_DEVICE(device);
345 unsigned int cmd = VGV3_NEXTCMD_JUMP << VGV3_NEXTCMD_NEXTCMD_FSHIFT;
346
Shubhraprakash Das8cbd2322012-03-04 14:41:39 -0700347 if (init_ram) {
348 z180_dev->timestamp = 0;
349 z180_dev->current_timestamp = 0;
350 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700351
352 addmarker(&z180_dev->ringbuffer, 0);
353
354 z180_cmdwindow_write(device, ADDR_VGV3_MODE, 4);
355
356 z180_cmdwindow_write(device, ADDR_VGV3_NEXTADDR,
357 z180_dev->ringbuffer.cmdbufdesc.gpuaddr);
358
359 z180_cmdwindow_write(device, ADDR_VGV3_NEXTCMD, cmd | 5);
360
361 z180_cmdwindow_write(device, ADDR_VGV3_WRITEADDR,
362 device->memstore.gpuaddr);
363
364 cmd = (int)(((1) & VGV3_CONTROL_MARKADD_FMASK)
365 << VGV3_CONTROL_MARKADD_FSHIFT);
366
367 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, cmd);
368
369 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, 0);
370}
371
372static int room_in_rb(struct z180_device *device)
373{
374 int ts_diff;
375
376 ts_diff = device->current_timestamp - device->timestamp;
377
378 return ts_diff < Z180_PACKET_COUNT;
379}
380
381static int z180_idle(struct kgsl_device *device, unsigned int timeout)
382{
383 int status = 0;
384 struct z180_device *z180_dev = Z180_DEVICE(device);
385
Jordan Crousee6239dd2011-11-17 13:39:21 -0700386 if (timestamp_cmp(z180_dev->current_timestamp,
387 z180_dev->timestamp) > 0)
Wei Zouc8c01632012-03-24 17:27:26 -0700388 status = z180_wait(device, z180_dev->current_timestamp,
389 timeout);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700390
391 if (status)
392 KGSL_DRV_ERR(device, "z180_waittimestamp() timed out\n");
393
394 return status;
395}
396
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700397int
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{
Tarun Karra9b92ccd2011-08-19 10:59:57 -0700405 long result = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406 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) {
Carter Cooperb90880d2011-10-26 14:38:02 -0600420 result = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700421 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 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600444 kgsl_setstate(device, kgsl_mmu_pt_get_flags(device->mmu.hwpagetable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445 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 "
Tarun Karra9b92ccd2011-08-19 10:59:57 -0700452 "failed: %ld\n", result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453 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);
Lucille Sylvester808eca22011-11-03 10:26:29 -0700465 kgsl_pwrscale_busy(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466
467 /* Make sure the next ringbuffer entry has a marker */
468 addmarker(&z180_dev->ringbuffer, nextindex);
469
470 nextaddr = z180_dev->ringbuffer.cmdbufdesc.gpuaddr
471 + rb_offset(nextindex);
472
473 tmp.hostptr = (void *)(tmp.hostptr +
474 (sizedwords * sizeof(unsigned int)));
475 tmp.size = 12;
476
477 kgsl_sharedmem_writel(&tmp, 4, nextaddr);
478 kgsl_sharedmem_writel(&tmp, 8, nextcnt);
479
480 /* sync memory before activating the hardware for the new command*/
481 mb();
482
483 cmd = (int)(((2) & VGV3_CONTROL_MARKADD_FMASK)
484 << VGV3_CONTROL_MARKADD_FSHIFT);
485
486 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, cmd);
487 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, 0);
488error:
Tarun Karra9b92ccd2011-08-19 10:59:57 -0700489 return (int)result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700490}
491
492static int z180_ringbuffer_init(struct kgsl_device *device)
493{
494 struct z180_device *z180_dev = Z180_DEVICE(device);
495 memset(&z180_dev->ringbuffer, 0, sizeof(struct z180_ringbuffer));
496 z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT;
497 return kgsl_allocate_contiguous(&z180_dev->ringbuffer.cmdbufdesc,
498 Z180_RB_SIZE);
499}
500
501static void z180_ringbuffer_close(struct kgsl_device *device)
502{
503 struct z180_device *z180_dev = Z180_DEVICE(device);
504 kgsl_sharedmem_free(&z180_dev->ringbuffer.cmdbufdesc);
505 memset(&z180_dev->ringbuffer, 0, sizeof(struct z180_ringbuffer));
506}
507
508static int __devinit z180_probe(struct platform_device *pdev)
509{
510 int status = -EINVAL;
511 struct kgsl_device *device = NULL;
512 struct z180_device *z180_dev;
513
514 device = (struct kgsl_device *)pdev->id_entry->driver_data;
515 device->parentdev = &pdev->dev;
516
517 z180_dev = Z180_DEVICE(device);
518 spin_lock_init(&z180_dev->cmdwin_lock);
519
520 status = z180_ringbuffer_init(device);
521 if (status != 0)
522 goto error;
523
524 status = kgsl_device_platform_probe(device, z180_isr);
525 if (status)
526 goto error_close_ringbuffer;
527
Lucille Sylvester591ea032011-07-21 16:08:37 -0600528 kgsl_pwrscale_init(device);
Lucille Sylvester808eca22011-11-03 10:26:29 -0700529 kgsl_pwrscale_attach_policy(device, Z180_DEFAULT_PWRSCALE_POLICY);
Lucille Sylvester591ea032011-07-21 16:08:37 -0600530
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531 return status;
532
533error_close_ringbuffer:
534 z180_ringbuffer_close(device);
535error:
536 device->parentdev = NULL;
537 return status;
538}
539
540static int __devexit z180_remove(struct platform_device *pdev)
541{
542 struct kgsl_device *device = NULL;
543
544 device = (struct kgsl_device *)pdev->id_entry->driver_data;
545
Lucille Sylvester591ea032011-07-21 16:08:37 -0600546 kgsl_pwrscale_close(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547 kgsl_device_platform_remove(device);
548
549 z180_ringbuffer_close(device);
550
551 return 0;
552}
553
554static int z180_start(struct kgsl_device *device, unsigned int init_ram)
555{
556 int status = 0;
557
Jeremy Gebben388c2972011-12-16 09:05:07 -0700558 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559
560 kgsl_pwrctrl_enable(device);
561
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562 /* Set interrupts to 0 to ensure a good state */
563 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0x0);
564
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600565 kgsl_mh_start(device);
566
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700567 status = kgsl_mmu_start(device);
568 if (status)
569 goto error_clk_off;
570
Shubhraprakash Das8cbd2322012-03-04 14:41:39 -0700571 z180_cmdstream_start(device, init_ram);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572
573 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jeremy Gebbenb46f4152011-10-14 14:27:00 -0600574 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700575 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576 return 0;
577
578error_clk_off:
579 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0);
580 kgsl_pwrctrl_disable(device);
581 return status;
582}
583
584static int z180_stop(struct kgsl_device *device)
585{
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700586 device->ftbl->irqctrl(device, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700587 z180_idle(device, KGSL_TIMEOUT_DEFAULT);
588
Suman Tatiraju4a32c652012-02-17 11:59:05 -0800589 del_timer_sync(&device->idle_timer);
590
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591 kgsl_mmu_stop(device);
592
593 /* Disable the clocks before the power rail. */
594 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
595
596 kgsl_pwrctrl_disable(device);
597
598 return 0;
599}
600
601static int z180_getproperty(struct kgsl_device *device,
602 enum kgsl_property_type type,
603 void *value,
604 unsigned int sizebytes)
605{
606 int status = -EINVAL;
607
608 switch (type) {
609 case KGSL_PROP_DEVICE_INFO:
610 {
611 struct kgsl_devinfo devinfo;
612
613 if (sizebytes != sizeof(devinfo)) {
614 status = -EINVAL;
615 break;
616 }
617
618 memset(&devinfo, 0, sizeof(devinfo));
619 devinfo.device_id = device->id+1;
620 devinfo.chip_id = 0;
621 devinfo.mmu_enabled = kgsl_mmu_enabled();
622
623 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
624 0) {
625 status = -EFAULT;
626 break;
627 }
628 status = 0;
629 }
630 break;
631 case KGSL_PROP_MMU_ENABLE:
632 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600633 int mmu_prop = kgsl_mmu_enabled();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700634 if (sizebytes != sizeof(int)) {
635 status = -EINVAL;
636 break;
637 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600638 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639 status = -EFAULT;
640 break;
641 }
642 status = 0;
643 }
644 break;
645
646 default:
647 KGSL_DRV_ERR(device, "invalid property: %d\n", type);
648 status = -EINVAL;
649 }
650 return status;
651}
652
653static unsigned int z180_isidle(struct kgsl_device *device)
654{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700655 struct z180_device *z180_dev = Z180_DEVICE(device);
656
Jordan Crousee6239dd2011-11-17 13:39:21 -0700657 return (timestamp_cmp(z180_dev->timestamp,
658 z180_dev->current_timestamp) == 0) ? true : false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700659}
660
661static int z180_suspend_context(struct kgsl_device *device)
662{
663 struct z180_device *z180_dev = Z180_DEVICE(device);
664
665 z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT;
666
667 return 0;
668}
669
670/* Not all Z180 registers are directly accessible.
671 * The _z180_(read|write)_simple functions below handle the ones that are.
672 */
673static void _z180_regread_simple(struct kgsl_device *device,
674 unsigned int offsetwords,
675 unsigned int *value)
676{
677 unsigned int *reg;
678
679 BUG_ON(offsetwords * sizeof(uint32_t) >= device->regspace.sizebytes);
680
681 reg = (unsigned int *)(device->regspace.mmio_virt_base
682 + (offsetwords << 2));
683
684 /*ensure this read finishes before the next one.
685 * i.e. act like normal readl() */
686 *value = __raw_readl(reg);
687 rmb();
688
689}
690
691static void _z180_regwrite_simple(struct kgsl_device *device,
692 unsigned int offsetwords,
693 unsigned int value)
694{
695 unsigned int *reg;
696
697 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
698
699 reg = (unsigned int *)(device->regspace.mmio_virt_base
700 + (offsetwords << 2));
701 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
702 /*ensure previous writes post before this one,
703 * i.e. act like normal writel() */
704 wmb();
705 __raw_writel(value, reg);
706}
707
708
709/* The MH registers must be accessed through via a 2 step write, (read|write)
710 * process. These registers may be accessed from interrupt context during
711 * the handling of MH or MMU error interrupts. Therefore a spin lock is used
712 * to ensure that the 2 step sequence is not interrupted.
713 */
714static void _z180_regread_mmu(struct kgsl_device *device,
715 unsigned int offsetwords,
716 unsigned int *value)
717{
718 struct z180_device *z180_dev = Z180_DEVICE(device);
719 unsigned long flags;
720
721 spin_lock_irqsave(&z180_dev->cmdwin_lock, flags);
722 _z180_regwrite_simple(device, (ADDR_VGC_MH_READ_ADDR >> 2),
723 offsetwords);
724 _z180_regread_simple(device, (ADDR_VGC_MH_DATA_ADDR >> 2), value);
725 spin_unlock_irqrestore(&z180_dev->cmdwin_lock, flags);
726}
727
728
729static void _z180_regwrite_mmu(struct kgsl_device *device,
730 unsigned int offsetwords,
731 unsigned int value)
732{
733 struct z180_device *z180_dev = Z180_DEVICE(device);
734 unsigned int cmdwinaddr;
735 unsigned long flags;
736
737 cmdwinaddr = ((Z180_CMDWINDOW_MMU << Z180_CMDWINDOW_TARGET_SHIFT) &
738 Z180_CMDWINDOW_TARGET_MASK);
739 cmdwinaddr |= ((offsetwords << Z180_CMDWINDOW_ADDR_SHIFT) &
740 Z180_CMDWINDOW_ADDR_MASK);
741
742 spin_lock_irqsave(&z180_dev->cmdwin_lock, flags);
743 _z180_regwrite_simple(device, ADDR_VGC_MMUCOMMANDSTREAM >> 2,
744 cmdwinaddr);
745 _z180_regwrite_simple(device, ADDR_VGC_MMUCOMMANDSTREAM >> 2, value);
746 spin_unlock_irqrestore(&z180_dev->cmdwin_lock, flags);
747}
748
749/* the rest of the code doesn't want to think about if it is writing mmu
750 * registers or normal registers so handle it here
751 */
752static void z180_regread(struct kgsl_device *device,
753 unsigned int offsetwords,
754 unsigned int *value)
755{
756 if (!in_interrupt())
757 kgsl_pre_hwaccess(device);
758
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600759 if ((offsetwords >= MH_ARBITER_CONFIG &&
760 offsetwords <= MH_AXI_HALT_CONTROL) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761 (offsetwords >= MH_MMU_CONFIG &&
762 offsetwords <= MH_MMU_MPU_END)) {
763 _z180_regread_mmu(device, offsetwords, value);
764 } else {
765 _z180_regread_simple(device, offsetwords, value);
766 }
767}
768
769static void z180_regwrite(struct kgsl_device *device,
770 unsigned int offsetwords,
771 unsigned int value)
772{
773 if (!in_interrupt())
774 kgsl_pre_hwaccess(device);
775
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600776 if ((offsetwords >= MH_ARBITER_CONFIG &&
777 offsetwords <= MH_CLNT_INTF_CTRL_CONFIG2) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700778 (offsetwords >= MH_MMU_CONFIG &&
779 offsetwords <= MH_MMU_MPU_END)) {
780 _z180_regwrite_mmu(device, offsetwords, value);
781 } else {
782 _z180_regwrite_simple(device, offsetwords, value);
783 }
784}
785
786static void z180_cmdwindow_write(struct kgsl_device *device,
787 unsigned int addr, unsigned int data)
788{
789 unsigned int cmdwinaddr;
790
791 cmdwinaddr = ((Z180_CMDWINDOW_2D << Z180_CMDWINDOW_TARGET_SHIFT) &
792 Z180_CMDWINDOW_TARGET_MASK);
793 cmdwinaddr |= ((addr << Z180_CMDWINDOW_ADDR_SHIFT) &
794 Z180_CMDWINDOW_ADDR_MASK);
795
796 z180_regwrite(device, ADDR_VGC_COMMANDSTREAM >> 2, cmdwinaddr);
797 z180_regwrite(device, ADDR_VGC_COMMANDSTREAM >> 2, data);
798}
799
800static unsigned int z180_readtimestamp(struct kgsl_device *device,
Wei Zouc8c01632012-03-24 17:27:26 -0700801 enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700802{
803 struct z180_device *z180_dev = Z180_DEVICE(device);
804 /* get current EOP timestamp */
805 return z180_dev->timestamp;
806}
807
808static int z180_waittimestamp(struct kgsl_device *device,
809 unsigned int timestamp,
810 unsigned int msecs)
811{
812 int status = -EINVAL;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530813
814 /* Don't wait forever, set a max (10 sec) value for now */
815 if (msecs == -1)
816 msecs = 10 * MSEC_PER_SEC;
817
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700818 mutex_unlock(&device->mutex);
Wei Zouc8c01632012-03-24 17:27:26 -0700819 status = z180_wait(device, timestamp, msecs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700820 mutex_lock(&device->mutex);
821
822 return status;
823}
824
825static int z180_wait(struct kgsl_device *device,
826 unsigned int timestamp,
827 unsigned int msecs)
828{
829 int status = -EINVAL;
830 long timeout = 0;
831
832 timeout = wait_io_event_interruptible_timeout(
833 device->wait_queue,
Wei Zouc8c01632012-03-24 17:27:26 -0700834 kgsl_check_timestamp(device, timestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700835 msecs_to_jiffies(msecs));
836
837 if (timeout > 0)
838 status = 0;
839 else if (timeout == 0) {
840 status = -ETIMEDOUT;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700841 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700842 } 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{
Lucille Sylvester591ea032011-07-21 16:08:37 -0600866 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lucille Sylvester808eca22011-11-03 10:26:29 -0700867 s64 tmp = ktime_to_us(ktime_get());
Lucille Sylvester591ea032011-07-21 16:08:37 -0600868
869 if (pwr->time == 0) {
Lucille Sylvester808eca22011-11-03 10:26:29 -0700870 pwr->time = tmp;
Lucille Sylvester591ea032011-07-21 16:08:37 -0600871 stats->total_time = 0;
872 stats->busy_time = 0;
873 } else {
Lucille Sylvester591ea032011-07-21 16:08:37 -0600874 stats->total_time = tmp - pwr->time;
Lucille Sylvester591ea032011-07-21 16:08:37 -0600875 pwr->time = tmp;
Lucille Sylvester808eca22011-11-03 10:26:29 -0700876 stats->busy_time = tmp - device->on_time;
877 device->on_time = tmp;
Lucille Sylvester591ea032011-07-21 16:08:37 -0600878 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700879}
880
881static void z180_irqctrl(struct kgsl_device *device, int state)
882{
883 /* Control interrupts for Z180 and the Z180 MMU */
884
885 if (state) {
886 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 3);
887 z180_regwrite(device, MH_INTERRUPT_MASK, KGSL_MMU_INT_MASK);
888 } else {
889 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0);
890 z180_regwrite(device, MH_INTERRUPT_MASK, 0);
891 }
892}
893
Jordan Crousea0758f22011-12-07 11:19:22 -0700894static unsigned int z180_gpuid(struct kgsl_device *device)
895{
896 /* Standard KGSL gpuid format:
897 * top word is 0x0002 for 2D or 0x0003 for 3D
898 * Bottom word is core specific identifer
899 */
900
901 return (0x0002 << 16) | 180;
902}
903
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904static const struct kgsl_functable z180_functable = {
905 /* Mandatory functions */
906 .regread = z180_regread,
907 .regwrite = z180_regwrite,
908 .idle = z180_idle,
909 .isidle = z180_isidle,
910 .suspend_context = z180_suspend_context,
911 .start = z180_start,
912 .stop = z180_stop,
913 .getproperty = z180_getproperty,
914 .waittimestamp = z180_waittimestamp,
915 .readtimestamp = z180_readtimestamp,
916 .issueibcmds = z180_cmdstream_issueibcmds,
917 .setup_pt = z180_setup_pt,
918 .cleanup_pt = z180_cleanup_pt,
919 .power_stats = z180_power_stats,
920 .irqctrl = z180_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -0700921 .gpuid = z180_gpuid,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922 /* Optional functions */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700923 .drawctxt_create = NULL,
924 .drawctxt_destroy = z180_drawctxt_destroy,
925 .ioctl = NULL,
926};
927
928static struct platform_device_id z180_id_table[] = {
929 { DEVICE_2D0_NAME, (kernel_ulong_t)&device_2d0.dev, },
930 { DEVICE_2D1_NAME, (kernel_ulong_t)&device_2d1.dev, },
931 { },
932};
933MODULE_DEVICE_TABLE(platform, z180_id_table);
934
935static struct platform_driver z180_platform_driver = {
936 .probe = z180_probe,
937 .remove = __devexit_p(z180_remove),
938 .suspend = kgsl_suspend_driver,
939 .resume = kgsl_resume_driver,
940 .id_table = z180_id_table,
941 .driver = {
942 .owner = THIS_MODULE,
943 .name = DEVICE_2D_NAME,
944 .pm = &kgsl_pm_ops,
945 }
946};
947
948static int __init kgsl_2d_init(void)
949{
950 return platform_driver_register(&z180_platform_driver);
951}
952
953static void __exit kgsl_2d_exit(void)
954{
955 platform_driver_unregister(&z180_platform_driver);
956}
957
958module_init(kgsl_2d_init);
959module_exit(kgsl_2d_exit);
960
961MODULE_DESCRIPTION("2D Graphics driver");
962MODULE_VERSION("1.2");
963MODULE_LICENSE("GPL v2");
964MODULE_ALIAS("platform:kgsl_2d");