blob: 41f4435dd1494bed94a5637ea1984164ca0dfc39 [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;
Jeremy Gebbend27b09f2012-04-06 16:09:13 -0600412 struct kgsl_mem_entry *entry = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700413 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;
Jeremy Gebbend27b09f2012-04-06 16:09:13 -0600430 /*
431 * Get a kernel mapping to the IB for monkey patching.
432 * See the end of this function.
433 */
434 entry = kgsl_sharedmem_find_region(dev_priv->process_priv, cmd,
435 sizedwords);
436 if (entry == NULL) {
437 KGSL_DRV_ERR(device, "Bad ibdesc: gpuaddr 0x%x size %d\n",
438 cmd, sizedwords);
439 result = -EINVAL;
440 goto error;
441 }
442 /*
443 * This will only map memory if it exists, otherwise it will reuse the
444 * mapping. And the 2d userspace reuses IBs so we likely won't create
445 * too many mappings.
446 */
447 if (kgsl_gpuaddr_to_vaddr(&entry->memdesc, cmd) == NULL) {
448 KGSL_DRV_ERR(device,
449 "Cannot make kernel mapping for gpuaddr 0x%x\n",
450 cmd);
451 result = -EINVAL;
452 goto error;
453 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454
455 KGSL_CMD_INFO(device, "ctxt %d ibaddr 0x%08x sizedwords %d\n",
456 context->id, cmd, sizedwords);
457 /* context switch */
458 if ((context->id != (int)z180_dev->ringbuffer.prevctx) ||
459 (ctrl & KGSL_CONTEXT_CTX_SWITCH)) {
460 KGSL_CMD_INFO(device, "context switch %d -> %d\n",
461 context->id, z180_dev->ringbuffer.prevctx);
462 kgsl_mmu_setstate(device, pagetable);
463 cnt = PACKETSIZE_STATESTREAM;
464 ofs = 0;
465 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600466 kgsl_setstate(device, kgsl_mmu_pt_get_flags(device->mmu.hwpagetable,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700467 device->id));
468
469 result = wait_event_interruptible_timeout(device->wait_queue,
470 room_in_rb(z180_dev),
471 msecs_to_jiffies(KGSL_TIMEOUT_DEFAULT));
472 if (result < 0) {
473 KGSL_CMD_ERR(device, "wait_event_interruptible_timeout "
Tarun Karra9b92ccd2011-08-19 10:59:57 -0700474 "failed: %ld\n", result);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475 goto error;
476 }
477 result = 0;
478
479 index = z180_dev->current_timestamp % Z180_PACKET_COUNT;
480 z180_dev->current_timestamp++;
481 nextindex = z180_dev->current_timestamp % Z180_PACKET_COUNT;
482 *timestamp = z180_dev->current_timestamp;
483
484 z180_dev->ringbuffer.prevctx = context->id;
485
486 addcmd(&z180_dev->ringbuffer, index, cmd + ofs, cnt);
Lucille Sylvester808eca22011-11-03 10:26:29 -0700487 kgsl_pwrscale_busy(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700488
489 /* Make sure the next ringbuffer entry has a marker */
490 addmarker(&z180_dev->ringbuffer, nextindex);
491
492 nextaddr = z180_dev->ringbuffer.cmdbufdesc.gpuaddr
493 + rb_offset(nextindex);
494
Jeremy Gebbend27b09f2012-04-06 16:09:13 -0600495 /* monkey patch the IB so that it jumps back to the ringbuffer */
496 kgsl_sharedmem_writel(&entry->memdesc,
497 ((sizedwords + 1) * sizeof(unsigned int)),
498 nextaddr);
499 kgsl_sharedmem_writel(&entry->memdesc,
500 ((sizedwords + 2) * sizeof(unsigned int)),
501 nextcnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502
503 /* sync memory before activating the hardware for the new command*/
504 mb();
505
506 cmd = (int)(((2) & VGV3_CONTROL_MARKADD_FMASK)
507 << VGV3_CONTROL_MARKADD_FSHIFT);
508
509 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, cmd);
510 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, 0);
511error:
Tarun Karra9b92ccd2011-08-19 10:59:57 -0700512 return (int)result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700513}
514
515static int z180_ringbuffer_init(struct kgsl_device *device)
516{
517 struct z180_device *z180_dev = Z180_DEVICE(device);
518 memset(&z180_dev->ringbuffer, 0, sizeof(struct z180_ringbuffer));
519 z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT;
520 return kgsl_allocate_contiguous(&z180_dev->ringbuffer.cmdbufdesc,
521 Z180_RB_SIZE);
522}
523
524static void z180_ringbuffer_close(struct kgsl_device *device)
525{
526 struct z180_device *z180_dev = Z180_DEVICE(device);
527 kgsl_sharedmem_free(&z180_dev->ringbuffer.cmdbufdesc);
528 memset(&z180_dev->ringbuffer, 0, sizeof(struct z180_ringbuffer));
529}
530
531static int __devinit z180_probe(struct platform_device *pdev)
532{
533 int status = -EINVAL;
534 struct kgsl_device *device = NULL;
535 struct z180_device *z180_dev;
536
537 device = (struct kgsl_device *)pdev->id_entry->driver_data;
538 device->parentdev = &pdev->dev;
539
540 z180_dev = Z180_DEVICE(device);
541 spin_lock_init(&z180_dev->cmdwin_lock);
542
543 status = z180_ringbuffer_init(device);
544 if (status != 0)
545 goto error;
546
547 status = kgsl_device_platform_probe(device, z180_isr);
548 if (status)
549 goto error_close_ringbuffer;
550
Lucille Sylvester591ea032011-07-21 16:08:37 -0600551 kgsl_pwrscale_init(device);
Lucille Sylvester808eca22011-11-03 10:26:29 -0700552 kgsl_pwrscale_attach_policy(device, Z180_DEFAULT_PWRSCALE_POLICY);
Lucille Sylvester591ea032011-07-21 16:08:37 -0600553
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554 return status;
555
556error_close_ringbuffer:
557 z180_ringbuffer_close(device);
558error:
559 device->parentdev = NULL;
560 return status;
561}
562
563static int __devexit z180_remove(struct platform_device *pdev)
564{
565 struct kgsl_device *device = NULL;
566
567 device = (struct kgsl_device *)pdev->id_entry->driver_data;
568
Lucille Sylvester591ea032011-07-21 16:08:37 -0600569 kgsl_pwrscale_close(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570 kgsl_device_platform_remove(device);
571
572 z180_ringbuffer_close(device);
573
574 return 0;
575}
576
577static int z180_start(struct kgsl_device *device, unsigned int init_ram)
578{
579 int status = 0;
580
Jeremy Gebben388c2972011-12-16 09:05:07 -0700581 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582
583 kgsl_pwrctrl_enable(device);
584
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585 /* Set interrupts to 0 to ensure a good state */
586 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0x0);
587
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600588 kgsl_mh_start(device);
589
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590 status = kgsl_mmu_start(device);
591 if (status)
592 goto error_clk_off;
593
Shubhraprakash Das8cbd2322012-03-04 14:41:39 -0700594 z180_cmdstream_start(device, init_ram);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595
596 mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT);
Jeremy Gebbenb46f4152011-10-14 14:27:00 -0600597 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700598 device->ftbl->irqctrl(device, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700599 return 0;
600
601error_clk_off:
602 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0);
603 kgsl_pwrctrl_disable(device);
604 return status;
605}
606
607static int z180_stop(struct kgsl_device *device)
608{
Jeremy Gebbenb7bc9552012-01-09 13:32:49 -0700609 device->ftbl->irqctrl(device, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700610 z180_idle(device, KGSL_TIMEOUT_DEFAULT);
611
Suman Tatiraju4a32c652012-02-17 11:59:05 -0800612 del_timer_sync(&device->idle_timer);
613
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700614 kgsl_mmu_stop(device);
615
616 /* Disable the clocks before the power rail. */
617 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
618
619 kgsl_pwrctrl_disable(device);
620
621 return 0;
622}
623
624static int z180_getproperty(struct kgsl_device *device,
625 enum kgsl_property_type type,
626 void *value,
627 unsigned int sizebytes)
628{
629 int status = -EINVAL;
630
631 switch (type) {
632 case KGSL_PROP_DEVICE_INFO:
633 {
634 struct kgsl_devinfo devinfo;
635
636 if (sizebytes != sizeof(devinfo)) {
637 status = -EINVAL;
638 break;
639 }
640
641 memset(&devinfo, 0, sizeof(devinfo));
642 devinfo.device_id = device->id+1;
643 devinfo.chip_id = 0;
644 devinfo.mmu_enabled = kgsl_mmu_enabled();
645
646 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
647 0) {
648 status = -EFAULT;
649 break;
650 }
651 status = 0;
652 }
653 break;
654 case KGSL_PROP_MMU_ENABLE:
655 {
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600656 int mmu_prop = kgsl_mmu_enabled();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657 if (sizebytes != sizeof(int)) {
658 status = -EINVAL;
659 break;
660 }
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600661 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700662 status = -EFAULT;
663 break;
664 }
665 status = 0;
666 }
667 break;
668
669 default:
670 KGSL_DRV_ERR(device, "invalid property: %d\n", type);
671 status = -EINVAL;
672 }
673 return status;
674}
675
676static unsigned int z180_isidle(struct kgsl_device *device)
677{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700678 struct z180_device *z180_dev = Z180_DEVICE(device);
679
Jordan Crousee6239dd2011-11-17 13:39:21 -0700680 return (timestamp_cmp(z180_dev->timestamp,
681 z180_dev->current_timestamp) == 0) ? true : false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700682}
683
684static int z180_suspend_context(struct kgsl_device *device)
685{
686 struct z180_device *z180_dev = Z180_DEVICE(device);
687
688 z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT;
689
690 return 0;
691}
692
693/* Not all Z180 registers are directly accessible.
694 * The _z180_(read|write)_simple functions below handle the ones that are.
695 */
696static void _z180_regread_simple(struct kgsl_device *device,
697 unsigned int offsetwords,
698 unsigned int *value)
699{
700 unsigned int *reg;
701
702 BUG_ON(offsetwords * sizeof(uint32_t) >= device->regspace.sizebytes);
703
704 reg = (unsigned int *)(device->regspace.mmio_virt_base
705 + (offsetwords << 2));
706
707 /*ensure this read finishes before the next one.
708 * i.e. act like normal readl() */
709 *value = __raw_readl(reg);
710 rmb();
711
712}
713
714static void _z180_regwrite_simple(struct kgsl_device *device,
715 unsigned int offsetwords,
716 unsigned int value)
717{
718 unsigned int *reg;
719
720 BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes);
721
722 reg = (unsigned int *)(device->regspace.mmio_virt_base
723 + (offsetwords << 2));
724 kgsl_cffdump_regwrite(device->id, offsetwords << 2, value);
725 /*ensure previous writes post before this one,
726 * i.e. act like normal writel() */
727 wmb();
728 __raw_writel(value, reg);
729}
730
731
732/* The MH registers must be accessed through via a 2 step write, (read|write)
733 * process. These registers may be accessed from interrupt context during
734 * the handling of MH or MMU error interrupts. Therefore a spin lock is used
735 * to ensure that the 2 step sequence is not interrupted.
736 */
737static void _z180_regread_mmu(struct kgsl_device *device,
738 unsigned int offsetwords,
739 unsigned int *value)
740{
741 struct z180_device *z180_dev = Z180_DEVICE(device);
742 unsigned long flags;
743
744 spin_lock_irqsave(&z180_dev->cmdwin_lock, flags);
745 _z180_regwrite_simple(device, (ADDR_VGC_MH_READ_ADDR >> 2),
746 offsetwords);
747 _z180_regread_simple(device, (ADDR_VGC_MH_DATA_ADDR >> 2), value);
748 spin_unlock_irqrestore(&z180_dev->cmdwin_lock, flags);
749}
750
751
752static void _z180_regwrite_mmu(struct kgsl_device *device,
753 unsigned int offsetwords,
754 unsigned int value)
755{
756 struct z180_device *z180_dev = Z180_DEVICE(device);
757 unsigned int cmdwinaddr;
758 unsigned long flags;
759
760 cmdwinaddr = ((Z180_CMDWINDOW_MMU << Z180_CMDWINDOW_TARGET_SHIFT) &
761 Z180_CMDWINDOW_TARGET_MASK);
762 cmdwinaddr |= ((offsetwords << Z180_CMDWINDOW_ADDR_SHIFT) &
763 Z180_CMDWINDOW_ADDR_MASK);
764
765 spin_lock_irqsave(&z180_dev->cmdwin_lock, flags);
766 _z180_regwrite_simple(device, ADDR_VGC_MMUCOMMANDSTREAM >> 2,
767 cmdwinaddr);
768 _z180_regwrite_simple(device, ADDR_VGC_MMUCOMMANDSTREAM >> 2, value);
769 spin_unlock_irqrestore(&z180_dev->cmdwin_lock, flags);
770}
771
772/* the rest of the code doesn't want to think about if it is writing mmu
773 * registers or normal registers so handle it here
774 */
775static void z180_regread(struct kgsl_device *device,
776 unsigned int offsetwords,
777 unsigned int *value)
778{
779 if (!in_interrupt())
780 kgsl_pre_hwaccess(device);
781
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600782 if ((offsetwords >= MH_ARBITER_CONFIG &&
783 offsetwords <= MH_AXI_HALT_CONTROL) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700784 (offsetwords >= MH_MMU_CONFIG &&
785 offsetwords <= MH_MMU_MPU_END)) {
786 _z180_regread_mmu(device, offsetwords, value);
787 } else {
788 _z180_regread_simple(device, offsetwords, value);
789 }
790}
791
792static void z180_regwrite(struct kgsl_device *device,
793 unsigned int offsetwords,
794 unsigned int value)
795{
796 if (!in_interrupt())
797 kgsl_pre_hwaccess(device);
798
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600799 if ((offsetwords >= MH_ARBITER_CONFIG &&
800 offsetwords <= MH_CLNT_INTF_CTRL_CONFIG2) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700801 (offsetwords >= MH_MMU_CONFIG &&
802 offsetwords <= MH_MMU_MPU_END)) {
803 _z180_regwrite_mmu(device, offsetwords, value);
804 } else {
805 _z180_regwrite_simple(device, offsetwords, value);
806 }
807}
808
809static void z180_cmdwindow_write(struct kgsl_device *device,
810 unsigned int addr, unsigned int data)
811{
812 unsigned int cmdwinaddr;
813
814 cmdwinaddr = ((Z180_CMDWINDOW_2D << Z180_CMDWINDOW_TARGET_SHIFT) &
815 Z180_CMDWINDOW_TARGET_MASK);
816 cmdwinaddr |= ((addr << Z180_CMDWINDOW_ADDR_SHIFT) &
817 Z180_CMDWINDOW_ADDR_MASK);
818
819 z180_regwrite(device, ADDR_VGC_COMMANDSTREAM >> 2, cmdwinaddr);
820 z180_regwrite(device, ADDR_VGC_COMMANDSTREAM >> 2, data);
821}
822
823static unsigned int z180_readtimestamp(struct kgsl_device *device,
Wei Zouc8c01632012-03-24 17:27:26 -0700824 enum kgsl_timestamp_type type)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825{
826 struct z180_device *z180_dev = Z180_DEVICE(device);
827 /* get current EOP timestamp */
828 return z180_dev->timestamp;
829}
830
831static int z180_waittimestamp(struct kgsl_device *device,
832 unsigned int timestamp,
833 unsigned int msecs)
834{
835 int status = -EINVAL;
Ranjhith Kalisamy823c1482011-09-05 20:31:07 +0530836
837 /* Don't wait forever, set a max (10 sec) value for now */
838 if (msecs == -1)
839 msecs = 10 * MSEC_PER_SEC;
840
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700841 mutex_unlock(&device->mutex);
Wei Zouc8c01632012-03-24 17:27:26 -0700842 status = z180_wait(device, timestamp, msecs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843 mutex_lock(&device->mutex);
844
845 return status;
846}
847
848static int z180_wait(struct kgsl_device *device,
849 unsigned int timestamp,
850 unsigned int msecs)
851{
852 int status = -EINVAL;
853 long timeout = 0;
854
855 timeout = wait_io_event_interruptible_timeout(
856 device->wait_queue,
Wei Zouc8c01632012-03-24 17:27:26 -0700857 kgsl_check_timestamp(device, timestamp),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700858 msecs_to_jiffies(msecs));
859
860 if (timeout > 0)
861 status = 0;
862 else if (timeout == 0) {
863 status = -ETIMEDOUT;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700864 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700865 } else
866 status = timeout;
867
868 return status;
869}
870
871static void
872z180_drawctxt_destroy(struct kgsl_device *device,
873 struct kgsl_context *context)
874{
875 struct z180_device *z180_dev = Z180_DEVICE(device);
876
877 z180_idle(device, KGSL_TIMEOUT_DEFAULT);
878
879 if (z180_dev->ringbuffer.prevctx == context->id) {
880 z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT;
881 device->mmu.hwpagetable = device->mmu.defaultpagetable;
882 kgsl_setstate(device, KGSL_MMUFLAGS_PTUPDATE);
883 }
884}
885
886static void z180_power_stats(struct kgsl_device *device,
887 struct kgsl_power_stats *stats)
888{
Lucille Sylvester591ea032011-07-21 16:08:37 -0600889 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
Lucille Sylvester808eca22011-11-03 10:26:29 -0700890 s64 tmp = ktime_to_us(ktime_get());
Lucille Sylvester591ea032011-07-21 16:08:37 -0600891
892 if (pwr->time == 0) {
Lucille Sylvester808eca22011-11-03 10:26:29 -0700893 pwr->time = tmp;
Lucille Sylvester591ea032011-07-21 16:08:37 -0600894 stats->total_time = 0;
895 stats->busy_time = 0;
896 } else {
Lucille Sylvester591ea032011-07-21 16:08:37 -0600897 stats->total_time = tmp - pwr->time;
Lucille Sylvester591ea032011-07-21 16:08:37 -0600898 pwr->time = tmp;
Lucille Sylvester808eca22011-11-03 10:26:29 -0700899 stats->busy_time = tmp - device->on_time;
900 device->on_time = tmp;
Lucille Sylvester591ea032011-07-21 16:08:37 -0600901 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902}
903
904static void z180_irqctrl(struct kgsl_device *device, int state)
905{
906 /* Control interrupts for Z180 and the Z180 MMU */
907
908 if (state) {
909 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 3);
910 z180_regwrite(device, MH_INTERRUPT_MASK, KGSL_MMU_INT_MASK);
911 } else {
912 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0);
913 z180_regwrite(device, MH_INTERRUPT_MASK, 0);
914 }
915}
916
Jordan Crousea0758f22011-12-07 11:19:22 -0700917static unsigned int z180_gpuid(struct kgsl_device *device)
918{
919 /* Standard KGSL gpuid format:
920 * top word is 0x0002 for 2D or 0x0003 for 3D
921 * Bottom word is core specific identifer
922 */
923
924 return (0x0002 << 16) | 180;
925}
926
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700927static const struct kgsl_functable z180_functable = {
928 /* Mandatory functions */
929 .regread = z180_regread,
930 .regwrite = z180_regwrite,
931 .idle = z180_idle,
932 .isidle = z180_isidle,
933 .suspend_context = z180_suspend_context,
934 .start = z180_start,
935 .stop = z180_stop,
936 .getproperty = z180_getproperty,
937 .waittimestamp = z180_waittimestamp,
938 .readtimestamp = z180_readtimestamp,
939 .issueibcmds = z180_cmdstream_issueibcmds,
940 .setup_pt = z180_setup_pt,
941 .cleanup_pt = z180_cleanup_pt,
942 .power_stats = z180_power_stats,
943 .irqctrl = z180_irqctrl,
Jordan Crousea0758f22011-12-07 11:19:22 -0700944 .gpuid = z180_gpuid,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700945 /* Optional functions */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946 .drawctxt_create = NULL,
947 .drawctxt_destroy = z180_drawctxt_destroy,
948 .ioctl = NULL,
949};
950
951static struct platform_device_id z180_id_table[] = {
952 { DEVICE_2D0_NAME, (kernel_ulong_t)&device_2d0.dev, },
953 { DEVICE_2D1_NAME, (kernel_ulong_t)&device_2d1.dev, },
954 { },
955};
956MODULE_DEVICE_TABLE(platform, z180_id_table);
957
958static struct platform_driver z180_platform_driver = {
959 .probe = z180_probe,
960 .remove = __devexit_p(z180_remove),
961 .suspend = kgsl_suspend_driver,
962 .resume = kgsl_resume_driver,
963 .id_table = z180_id_table,
964 .driver = {
965 .owner = THIS_MODULE,
966 .name = DEVICE_2D_NAME,
967 .pm = &kgsl_pm_ops,
968 }
969};
970
971static int __init kgsl_2d_init(void)
972{
973 return platform_driver_register(&z180_platform_driver);
974}
975
976static void __exit kgsl_2d_exit(void)
977{
978 platform_driver_unregister(&z180_platform_driver);
979}
980
981module_init(kgsl_2d_init);
982module_exit(kgsl_2d_exit);
983
984MODULE_DESCRIPTION("2D Graphics driver");
985MODULE_VERSION("1.2");
986MODULE_LICENSE("GPL v2");
987MODULE_ALIAS("platform:kgsl_2d");