blob: ae7aee0a0f591fbf02f693778514dab3e134d42a [file] [log] [blame]
Steve Kondikf7652b32013-11-26 15:20:51 -08001/* Copyright (c) 2002,2007-2013, The Linux Foundation. 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/module.h>
14#include <linux/uaccess.h>
15#include <linux/sched.h>
16
17#include "kgsl.h"
18#include "kgsl_cffdump.h"
19#include "kgsl_sharedmem.h"
20
21#include "z180.h"
22#include "z180_reg.h"
23#include "z180_trace.h"
24
25#define DRIVER_VERSION_MAJOR 3
26#define DRIVER_VERSION_MINOR 1
27
28#define GSL_VGC_INT_MASK \
29 (REG_VGC_IRQSTATUS__MH_MASK | \
30 REG_VGC_IRQSTATUS__G2D_MASK | \
31 REG_VGC_IRQSTATUS__FIFO_MASK)
32
33#define VGV3_NEXTCMD_JUMP 0x01
34
35#define VGV3_NEXTCMD_NEXTCMD_FSHIFT 12
36#define VGV3_NEXTCMD_NEXTCMD_FMASK 0x7
37
38#define VGV3_CONTROL_MARKADD_FSHIFT 0
39#define VGV3_CONTROL_MARKADD_FMASK 0xfff
40
41#define Z180_MARKER_SIZE 10
42#define Z180_CALL_CMD 0x1000
43#define Z180_MARKER_CMD 0x8000
44#define Z180_STREAM_END_CMD 0x9000
45#define Z180_STREAM_PACKET 0x7C000176
46#define Z180_STREAM_PACKET_CALL 0x7C000275
47
48#define NUMTEXUNITS 4
49#define TEXUNITREGCOUNT 25
50#define VG_REGCOUNT 0x39
51
52#define PACKETSIZE_BEGIN 3
53#define PACKETSIZE_G2DCOLOR 2
54#define PACKETSIZE_TEXUNIT (TEXUNITREGCOUNT * 2)
55#define PACKETSIZE_REG (VG_REGCOUNT * 2)
56#define PACKETSIZE_STATE (PACKETSIZE_TEXUNIT * NUMTEXUNITS + \
57 PACKETSIZE_REG + PACKETSIZE_BEGIN + \
58 PACKETSIZE_G2DCOLOR)
59#define PACKETSIZE_STATESTREAM (ALIGN((PACKETSIZE_STATE * \
60 sizeof(unsigned int)), 32) / \
61 sizeof(unsigned int))
62
63#define Z180_INVALID_CONTEXT UINT_MAX
64
65/* z180 MH arbiter config*/
66#define Z180_CFG_MHARB \
67 (0x10 \
68 | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \
69 | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \
70 | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \
71 | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \
72 | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \
73 | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \
74 | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \
75 | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \
76 | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \
77 | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \
78 | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \
79 | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \
80 | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \
81 | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT))
82
83#define Z180_TIMESTAMP_EPSILON 20000
84#define Z180_IDLE_COUNT_MAX 1000000
85
86enum z180_cmdwindow_type {
87 Z180_CMDWINDOW_2D = 0x00000000,
88 Z180_CMDWINDOW_MMU = 0x00000002,
89};
90
91#define Z180_CMDWINDOW_TARGET_MASK 0x000000FF
92#define Z180_CMDWINDOW_ADDR_MASK 0x00FFFF00
93#define Z180_CMDWINDOW_TARGET_SHIFT 0
94#define Z180_CMDWINDOW_ADDR_SHIFT 8
95
96static int z180_init(struct kgsl_device *device);
97static int z180_start(struct kgsl_device *device);
98static int z180_stop(struct kgsl_device *device);
99static int z180_wait(struct kgsl_device *device,
100 struct kgsl_context *context,
101 unsigned int timestamp,
102 unsigned int msecs);
103static void z180_regread(struct kgsl_device *device,
104 unsigned int offsetwords,
105 unsigned int *value);
106static void z180_regwrite(struct kgsl_device *device,
107 unsigned int offsetwords,
108 unsigned int value);
109static void z180_cmdwindow_write(struct kgsl_device *device,
110 unsigned int addr,
111 unsigned int data);
112
113#define Z180_MMU_CONFIG \
114 (0x01 \
115 | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \
116 | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \
117 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \
118 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \
119 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \
120 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \
121 | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \
122 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \
123 | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \
124 | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \
125 | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT))
126
127#define KGSL_LOG_LEVEL_DEFAULT 3
128
129static const struct kgsl_functable z180_functable;
130
131static struct z180_device device_2d0 = {
132 .dev = {
133 KGSL_DEVICE_COMMON_INIT(device_2d0.dev),
134 .name = DEVICE_2D0_NAME,
135 .id = KGSL_DEVICE_2D0,
136 .mh = {
137 .mharb = Z180_CFG_MHARB,
138 .mh_intf_cfg1 = 0x00032f07,
139 .mh_intf_cfg2 = 0x004b274f,
140 /* turn off memory protection unit by setting
141 acceptable physical address range to include
142 all pages. */
143 .mpu_base = 0x00000000,
144 .mpu_range = 0xFFFFF000,
145 },
146 .mmu = {
147 .config = Z180_MMU_CONFIG,
148 },
149 .pwrctrl = {
150 .irq_name = KGSL_2D0_IRQ,
151 },
152 .iomemname = KGSL_2D0_REG_MEMORY,
153 .ftbl = &z180_functable,
154 .cmd_log = KGSL_LOG_LEVEL_DEFAULT,
155 .ctxt_log = KGSL_LOG_LEVEL_DEFAULT,
156 .drv_log = KGSL_LOG_LEVEL_DEFAULT,
157 .mem_log = KGSL_LOG_LEVEL_DEFAULT,
158 .pwr_log = KGSL_LOG_LEVEL_DEFAULT,
159 .pm_dump_enable = 0,
160 },
161 .cmdwin_lock = __SPIN_LOCK_INITIALIZER(device_2d1.cmdwin_lock),
162};
163
164static struct z180_device device_2d1 = {
165 .dev = {
166 KGSL_DEVICE_COMMON_INIT(device_2d1.dev),
167 .name = DEVICE_2D1_NAME,
168 .id = KGSL_DEVICE_2D1,
169 .mh = {
170 .mharb = Z180_CFG_MHARB,
171 .mh_intf_cfg1 = 0x00032f07,
172 .mh_intf_cfg2 = 0x004b274f,
173 /* turn off memory protection unit by setting
174 acceptable physical address range to include
175 all pages. */
176 .mpu_base = 0x00000000,
177 .mpu_range = 0xFFFFF000,
178 },
179 .mmu = {
180 .config = Z180_MMU_CONFIG,
181 },
182 .pwrctrl = {
183 .irq_name = KGSL_2D1_IRQ,
184 },
185 .iomemname = KGSL_2D1_REG_MEMORY,
186 .ftbl = &z180_functable,
187 },
188 .cmdwin_lock = __SPIN_LOCK_INITIALIZER(device_2d1.cmdwin_lock),
189};
190
191static irqreturn_t z180_irq_handler(struct kgsl_device *device)
192{
193 irqreturn_t result = IRQ_NONE;
194 unsigned int status;
195 struct z180_device *z180_dev = Z180_DEVICE(device);
196
197 z180_regread(device, ADDR_VGC_IRQSTATUS >> 2, &status);
198
199 trace_kgsl_z180_irq_status(device, status);
200
201 if (status & GSL_VGC_INT_MASK) {
202 z180_regwrite(device,
203 ADDR_VGC_IRQSTATUS >> 2, status & GSL_VGC_INT_MASK);
204
205 result = IRQ_HANDLED;
206
207 if (status & REG_VGC_IRQSTATUS__FIFO_MASK)
208 KGSL_DRV_ERR(device, "z180 fifo interrupt\n");
209 if (status & REG_VGC_IRQSTATUS__MH_MASK)
210 kgsl_mh_intrcallback(device);
211 if (status & REG_VGC_IRQSTATUS__G2D_MASK) {
212 int count;
213
214 z180_regread(device,
215 ADDR_VGC_IRQ_ACTIVE_CNT >> 2,
216 &count);
217
218 count >>= 8;
219 count &= 255;
220 z180_dev->timestamp += count;
221
222 queue_work(device->work_queue, &device->ts_expired_ws);
223 wake_up_interruptible(&device->wait_queue);
224 }
225 }
226
227 if (device->requested_state == KGSL_STATE_NONE) {
228 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
229 queue_work(device->work_queue, &device->idle_check_ws);
230 }
231 mod_timer_pending(&device->idle_timer,
232 jiffies + device->pwrctrl.interval_timeout);
233
234 return result;
235}
236
237static void z180_cleanup_pt(struct kgsl_device *device,
238 struct kgsl_pagetable *pagetable)
239{
240 struct z180_device *z180_dev = Z180_DEVICE(device);
241
242 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
243
244 kgsl_mmu_unmap(pagetable, &device->memstore);
245
246 kgsl_mmu_unmap(pagetable, &z180_dev->ringbuffer.cmdbufdesc);
247}
248
249static int z180_setup_pt(struct kgsl_device *device,
250 struct kgsl_pagetable *pagetable)
251{
252 int result = 0;
253 struct z180_device *z180_dev = Z180_DEVICE(device);
254
255 result = kgsl_mmu_map_global(pagetable, &device->mmu.setstate_memory);
256
257 if (result)
258 goto error;
259
260 result = kgsl_mmu_map_global(pagetable, &device->memstore);
261 if (result)
262 goto error_unmap_dummy;
263
264 result = kgsl_mmu_map_global(pagetable,
265 &z180_dev->ringbuffer.cmdbufdesc);
266 if (result)
267 goto error_unmap_memstore;
268 /*
269 * Set the mpu end to the last "normal" global memory we use.
270 * For the IOMMU, this will be used to restrict access to the
271 * mapped registers.
272 */
273 device->mh.mpu_range = z180_dev->ringbuffer.cmdbufdesc.gpuaddr +
274 z180_dev->ringbuffer.cmdbufdesc.size;
275 return result;
276
277error_unmap_dummy:
278 kgsl_mmu_unmap(pagetable, &device->mmu.setstate_memory);
279
280error_unmap_memstore:
281 kgsl_mmu_unmap(pagetable, &device->memstore);
282
283error:
284 return result;
285}
286
287static inline unsigned int rb_offset(unsigned int timestamp)
288{
289 return (timestamp % Z180_PACKET_COUNT)
290 *sizeof(unsigned int)*(Z180_PACKET_SIZE);
291}
292
293static inline unsigned int rb_gpuaddr(struct z180_device *z180_dev,
294 unsigned int timestamp)
295{
296 return z180_dev->ringbuffer.cmdbufdesc.gpuaddr + rb_offset(timestamp);
297}
298
299static void addmarker(struct z180_ringbuffer *rb, unsigned int timestamp)
300{
301 char *ptr = (char *)(rb->cmdbufdesc.hostptr);
302 unsigned int *p = (unsigned int *)(ptr + rb_offset(timestamp));
303
304 *p++ = Z180_STREAM_PACKET;
305 *p++ = (Z180_MARKER_CMD | 5);
306 *p++ = ADDR_VGV3_LAST << 24;
307 *p++ = ADDR_VGV3_LAST << 24;
308 *p++ = ADDR_VGV3_LAST << 24;
309 *p++ = Z180_STREAM_PACKET;
310 *p++ = 5;
311 *p++ = ADDR_VGV3_LAST << 24;
312 *p++ = ADDR_VGV3_LAST << 24;
313 *p++ = ADDR_VGV3_LAST << 24;
314}
315
316static void addcmd(struct z180_ringbuffer *rb, unsigned int timestamp,
317 unsigned int cmd, unsigned int nextcnt)
318{
319 char * ptr = (char *)(rb->cmdbufdesc.hostptr);
320 unsigned int *p = (unsigned int *)(ptr + (rb_offset(timestamp)
321 + (Z180_MARKER_SIZE * sizeof(unsigned int))));
322
323 *p++ = Z180_STREAM_PACKET_CALL;
324 *p++ = cmd;
325 *p++ = Z180_CALL_CMD | nextcnt;
326 *p++ = ADDR_VGV3_LAST << 24;
327 *p++ = ADDR_VGV3_LAST << 24;
328}
329
330static void z180_cmdstream_start(struct kgsl_device *device)
331{
332 struct z180_device *z180_dev = Z180_DEVICE(device);
333 unsigned int cmd = VGV3_NEXTCMD_JUMP << VGV3_NEXTCMD_NEXTCMD_FSHIFT;
334
335 addmarker(&z180_dev->ringbuffer, 0);
336
337 z180_cmdwindow_write(device, ADDR_VGV3_MODE, 4);
338
339 z180_cmdwindow_write(device, ADDR_VGV3_NEXTADDR,
340 rb_gpuaddr(z180_dev, z180_dev->current_timestamp));
341
342 z180_cmdwindow_write(device, ADDR_VGV3_NEXTCMD, cmd | 5);
343
344 z180_cmdwindow_write(device, ADDR_VGV3_WRITEADDR,
345 device->memstore.gpuaddr);
346
347 cmd = (int)(((1) & VGV3_CONTROL_MARKADD_FMASK)
348 << VGV3_CONTROL_MARKADD_FSHIFT);
349
350 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, cmd);
351
352 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, 0);
353}
354
355static int room_in_rb(struct z180_device *device)
356{
357 int ts_diff;
358
359 ts_diff = device->current_timestamp - device->timestamp;
360
361 return ts_diff < Z180_PACKET_COUNT;
362}
363
364/**
365 * z180_idle() - Idle the 2D device
366 * @device: Pointer to the KGSL device struct for the Z180
367 *
368 * wait until the z180 submission queue is idle
369 */
370int z180_idle(struct kgsl_device *device)
371{
372 int status = 0;
373 struct z180_device *z180_dev = Z180_DEVICE(device);
374
375 if (timestamp_cmp(z180_dev->current_timestamp,
376 z180_dev->timestamp) > 0)
377 status = z180_wait(device, NULL,
378 z180_dev->current_timestamp,
379 Z180_IDLE_TIMEOUT);
380
381 if (status)
382 KGSL_DRV_ERR(device, "z180_waittimestamp() timed out\n");
383
384 return status;
385}
386
387int
388z180_cmdstream_issueibcmds(struct kgsl_device_private *dev_priv,
389 struct kgsl_context *context,
390 struct kgsl_cmdbatch *cmdbatch,
391 uint32_t *timestamp)
392{
393 long result = 0;
394 unsigned int ofs = PACKETSIZE_STATESTREAM * sizeof(unsigned int);
395 unsigned int cnt = 5;
396 unsigned int old_timestamp = 0;
397 unsigned int nextcnt = Z180_STREAM_END_CMD | 5;
398 struct kgsl_mem_entry *entry = NULL;
399 unsigned int cmd;
400 struct kgsl_device *device = dev_priv->device;
401 struct kgsl_pagetable *pagetable = dev_priv->process_priv->pagetable;
402 struct z180_device *z180_dev = Z180_DEVICE(device);
403 unsigned int sizedwords;
404 unsigned int numibs;
405 struct kgsl_ibdesc *ibdesc;
406
407 mutex_lock(&device->mutex);
408
Steve Kondik2deaf4b2014-03-20 22:29:09 -0700409 result = kgsl_active_count_get(device);
410 if (result)
411 goto error_active_count;
Steve Kondikf7652b32013-11-26 15:20:51 -0800412
413 if (cmdbatch == NULL) {
414 result = EINVAL;
415 goto error;
416 }
417
418 ibdesc = cmdbatch->ibdesc;
419 numibs = cmdbatch->ibcount;
420
421 if (device->state & KGSL_STATE_HUNG) {
422 result = -EINVAL;
423 goto error;
424 }
425 if (numibs != 1) {
426 KGSL_DRV_ERR(device, "Invalid number of ibs: %d\n", numibs);
427 result = -EINVAL;
428 goto error;
429 }
430 cmd = ibdesc[0].gpuaddr;
431 sizedwords = ibdesc[0].sizedwords;
432 /*
433 * Get a kernel mapping to the IB for monkey patching.
434 * See the end of this function.
435 */
436 entry = kgsl_sharedmem_find_region(dev_priv->process_priv, cmd,
437 sizedwords);
438 if (entry == NULL) {
439 KGSL_DRV_ERR(device, "Bad ibdesc: gpuaddr 0x%x size %d\n",
440 cmd, sizedwords);
441 result = -EINVAL;
442 goto error;
443 }
444 /*
445 * This will only map memory if it exists, otherwise it will reuse the
446 * mapping. And the 2d userspace reuses IBs so we likely won't create
447 * too many mappings.
448 */
449 if (kgsl_gpuaddr_to_vaddr(&entry->memdesc, cmd) == NULL) {
450 KGSL_DRV_ERR(device,
451 "Cannot make kernel mapping for gpuaddr 0x%x\n",
452 cmd);
453 result = -EINVAL;
454 goto error;
455 }
456
457 KGSL_CMD_INFO(device, "ctxt %d ibaddr 0x%08x sizedwords %d\n",
458 context->id, cmd, sizedwords);
459 /* context switch */
460 if ((context->id != (int)z180_dev->ringbuffer.prevctx) ||
461 (cmdbatch->flags & KGSL_CONTEXT_CTX_SWITCH)) {
462 KGSL_CMD_INFO(device, "context switch %d -> %d\n",
463 context->id, z180_dev->ringbuffer.prevctx);
464 kgsl_mmu_setstate(&device->mmu, pagetable,
465 KGSL_MEMSTORE_GLOBAL);
466 cnt = PACKETSIZE_STATESTREAM;
467 ofs = 0;
468 }
469
470 result = kgsl_setstate(&device->mmu,
471 KGSL_MEMSTORE_GLOBAL,
472 kgsl_mmu_pt_get_flags(device->mmu.hwpagetable,
473 device->id));
474 if (result < 0)
475 goto error;
476
477 result = wait_event_interruptible_timeout(device->wait_queue,
478 room_in_rb(z180_dev),
479 msecs_to_jiffies(KGSL_TIMEOUT_DEFAULT));
480 if (result < 0) {
481 KGSL_CMD_ERR(device, "wait_event_interruptible_timeout "
482 "failed: %ld\n", result);
483 goto error;
484 }
485 result = 0;
486
487 old_timestamp = z180_dev->current_timestamp;
488 z180_dev->current_timestamp++;
489 *timestamp = z180_dev->current_timestamp;
490
491 z180_dev->ringbuffer.prevctx = context->id;
492
493 addcmd(&z180_dev->ringbuffer, old_timestamp, cmd + ofs, cnt);
494 kgsl_pwrscale_busy(device);
495
496 /* Make sure the next ringbuffer entry has a marker */
497 addmarker(&z180_dev->ringbuffer, z180_dev->current_timestamp);
498
499 /* monkey patch the IB so that it jumps back to the ringbuffer */
500 kgsl_sharedmem_writel(device, &entry->memdesc,
501 ((sizedwords + 1) * sizeof(unsigned int)),
502 rb_gpuaddr(z180_dev, z180_dev->current_timestamp));
503 kgsl_sharedmem_writel(device, &entry->memdesc,
504 ((sizedwords + 2) * sizeof(unsigned int)),
505 nextcnt);
506
507 /* sync memory before activating the hardware for the new command*/
508 mb();
509
510 cmd = (int)(((2) & VGV3_CONTROL_MARKADD_FMASK)
511 << VGV3_CONTROL_MARKADD_FSHIFT);
512
513 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, cmd);
514 z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, 0);
515error:
516 kgsl_trace_issueibcmds(device, context->id, cmdbatch,
Ethan Chen7b185902014-11-16 16:48:31 -0800517 *timestamp, cmdbatch ? cmdbatch->flags : 0, result, 0);
Steve Kondikf7652b32013-11-26 15:20:51 -0800518
519 kgsl_active_count_put(device);
Steve Kondik2deaf4b2014-03-20 22:29:09 -0700520error_active_count:
Steve Kondikf7652b32013-11-26 15:20:51 -0800521 mutex_unlock(&device->mutex);
522
523 return (int)result;
524}
525
526static int z180_ringbuffer_init(struct kgsl_device *device)
527{
528 struct z180_device *z180_dev = Z180_DEVICE(device);
529 memset(&z180_dev->ringbuffer, 0, sizeof(struct z180_ringbuffer));
530 z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT;
531 z180_dev->ringbuffer.cmdbufdesc.flags = KGSL_MEMFLAGS_GPUREADONLY;
532 return kgsl_allocate_contiguous(&z180_dev->ringbuffer.cmdbufdesc,
533 Z180_RB_SIZE);
534}
535
536static void z180_ringbuffer_close(struct kgsl_device *device)
537{
538 struct z180_device *z180_dev = Z180_DEVICE(device);
539 kgsl_sharedmem_free(&z180_dev->ringbuffer.cmdbufdesc);
540 memset(&z180_dev->ringbuffer, 0, sizeof(struct z180_ringbuffer));
541}
542
543static int __devinit z180_probe(struct platform_device *pdev)
544{
545 int status = -EINVAL;
546 struct kgsl_device *device = NULL;
547 struct z180_device *z180_dev;
548
549 device = (struct kgsl_device *)pdev->id_entry->driver_data;
550 device->parentdev = &pdev->dev;
551
552 z180_dev = Z180_DEVICE(device);
553
554 status = z180_ringbuffer_init(device);
555 if (status != 0)
556 goto error;
557
558 status = kgsl_device_platform_probe(device);
559 if (status)
560 goto error_close_ringbuffer;
561
562 kgsl_pwrscale_init(device);
563 kgsl_pwrscale_attach_policy(device, Z180_DEFAULT_PWRSCALE_POLICY);
564
565 return status;
566
567error_close_ringbuffer:
568 z180_ringbuffer_close(device);
569error:
570 device->parentdev = NULL;
571 return status;
572}
573
574static int __devexit z180_remove(struct platform_device *pdev)
575{
576 struct kgsl_device *device = NULL;
577
578 device = (struct kgsl_device *)pdev->id_entry->driver_data;
579
580 kgsl_pwrscale_close(device);
581 kgsl_device_platform_remove(device);
582
583 z180_ringbuffer_close(device);
584
585 return 0;
586}
587
588static int z180_init(struct kgsl_device *device)
589{
590 struct z180_device *z180_dev = Z180_DEVICE(device);
591
592 z180_dev->timestamp = 0;
593 z180_dev->current_timestamp = 0;
594
595 return 0;
596}
597
598static int z180_start(struct kgsl_device *device)
599{
600 int status = 0;
601
602 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
603
604 kgsl_pwrctrl_enable(device);
605
606 /* Set interrupts to 0 to ensure a good state */
607 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0x0);
608
609 kgsl_mh_start(device);
610
611 status = kgsl_mmu_start(device);
612 if (status)
613 goto error_clk_off;
614
615 z180_cmdstream_start(device);
616
Steve Kondikf7652b32013-11-26 15:20:51 -0800617 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON);
618 device->ftbl->irqctrl(device, 1);
619
620 device->reset_counter++;
621
622 return 0;
623
624error_clk_off:
625 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0);
626 kgsl_pwrctrl_disable(device);
627 return status;
628}
629
630static int z180_stop(struct kgsl_device *device)
631{
632 int ret;
633
634 device->ftbl->irqctrl(device, 0);
635 ret = z180_idle(device);
636 if (ret)
637 return ret;
638
639 del_timer_sync(&device->idle_timer);
640
641 kgsl_mmu_stop(&device->mmu);
642
643 /* Disable the clocks before the power rail. */
644 kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF);
645
646 kgsl_pwrctrl_disable(device);
647
648 return 0;
649}
650
651static int z180_getproperty(struct kgsl_device *device,
652 enum kgsl_property_type type,
653 void *value,
654 unsigned int sizebytes)
655{
656 int status = -EINVAL;
657
658 switch (type) {
659 case KGSL_PROP_DEVICE_INFO:
660 {
661 struct kgsl_devinfo devinfo;
662
663 if (sizebytes != sizeof(devinfo)) {
664 status = -EINVAL;
665 break;
666 }
667
668 memset(&devinfo, 0, sizeof(devinfo));
669 devinfo.device_id = device->id+1;
670 devinfo.chip_id = 0;
671 devinfo.mmu_enabled = kgsl_mmu_enabled();
672
673 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
674 0) {
675 status = -EFAULT;
676 break;
677 }
678 status = 0;
679 }
680 break;
681 case KGSL_PROP_MMU_ENABLE:
682 {
683 int mmu_prop = kgsl_mmu_enabled();
684 if (sizebytes != sizeof(int)) {
685 status = -EINVAL;
686 break;
687 }
688 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
689 status = -EFAULT;
690 break;
691 }
692 status = 0;
693 }
694 break;
695
696 default:
697 KGSL_DRV_ERR(device, "invalid property: %d\n", type);
698 status = -EINVAL;
699 }
700 return status;
701}
702
703static bool z180_isidle(struct kgsl_device *device)
704{
705 struct z180_device *z180_dev = Z180_DEVICE(device);
706
707 return (timestamp_cmp(z180_dev->timestamp,
708 z180_dev->current_timestamp) == 0) ? true : false;
709}
710
711static int z180_suspend_context(struct kgsl_device *device)
712{
713 struct z180_device *z180_dev = Z180_DEVICE(device);
714
715 z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT;
716
717 return 0;
718}
719
720/* Not all Z180 registers are directly accessible.
721 * The _z180_(read|write)_simple functions below handle the ones that are.
722 */
723static void _z180_regread_simple(struct kgsl_device *device,
724 unsigned int offsetwords,
725 unsigned int *value)
726{
727 unsigned int *reg;
728
729 BUG_ON(offsetwords * sizeof(uint32_t) >= device->reg_len);
730
731 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
732
733 /*ensure this read finishes before the next one.
734 * i.e. act like normal readl() */
735 *value = __raw_readl(reg);
736 rmb();
737
738}
739
740static void _z180_regwrite_simple(struct kgsl_device *device,
741 unsigned int offsetwords,
742 unsigned int value)
743{
744 unsigned int *reg;
745
746 BUG_ON(offsetwords*sizeof(uint32_t) >= device->reg_len);
747
748 reg = (unsigned int *)(device->reg_virt + (offsetwords << 2));
749 kgsl_cffdump_regwrite(device, offsetwords << 2, value);
750 /*ensure previous writes post before this one,
751 * i.e. act like normal writel() */
752 wmb();
753 __raw_writel(value, reg);
754}
755
756
757/* The MH registers must be accessed through via a 2 step write, (read|write)
758 * process. These registers may be accessed from interrupt context during
759 * the handling of MH or MMU error interrupts. Therefore a spin lock is used
760 * to ensure that the 2 step sequence is not interrupted.
761 */
762static void _z180_regread_mmu(struct kgsl_device *device,
763 unsigned int offsetwords,
764 unsigned int *value)
765{
766 struct z180_device *z180_dev = Z180_DEVICE(device);
767 unsigned long flags;
768
769 spin_lock_irqsave(&z180_dev->cmdwin_lock, flags);
770 _z180_regwrite_simple(device, (ADDR_VGC_MH_READ_ADDR >> 2),
771 offsetwords);
772 _z180_regread_simple(device, (ADDR_VGC_MH_DATA_ADDR >> 2), value);
773 spin_unlock_irqrestore(&z180_dev->cmdwin_lock, flags);
774}
775
776
777static void _z180_regwrite_mmu(struct kgsl_device *device,
778 unsigned int offsetwords,
779 unsigned int value)
780{
781 struct z180_device *z180_dev = Z180_DEVICE(device);
782 unsigned int cmdwinaddr;
783 unsigned long flags;
784
785 cmdwinaddr = ((Z180_CMDWINDOW_MMU << Z180_CMDWINDOW_TARGET_SHIFT) &
786 Z180_CMDWINDOW_TARGET_MASK);
787 cmdwinaddr |= ((offsetwords << Z180_CMDWINDOW_ADDR_SHIFT) &
788 Z180_CMDWINDOW_ADDR_MASK);
789
790 spin_lock_irqsave(&z180_dev->cmdwin_lock, flags);
791 _z180_regwrite_simple(device, ADDR_VGC_MMUCOMMANDSTREAM >> 2,
792 cmdwinaddr);
793 _z180_regwrite_simple(device, ADDR_VGC_MMUCOMMANDSTREAM >> 2, value);
794 spin_unlock_irqrestore(&z180_dev->cmdwin_lock, flags);
795}
796
797/* the rest of the code doesn't want to think about if it is writing mmu
798 * registers or normal registers so handle it here
799 */
800static void z180_regread(struct kgsl_device *device,
801 unsigned int offsetwords,
802 unsigned int *value)
803{
804 if (!in_interrupt())
805 kgsl_pre_hwaccess(device);
806
807 if ((offsetwords >= MH_ARBITER_CONFIG &&
808 offsetwords <= MH_AXI_HALT_CONTROL) ||
809 (offsetwords >= MH_MMU_CONFIG &&
810 offsetwords <= MH_MMU_MPU_END)) {
811 _z180_regread_mmu(device, offsetwords, value);
812 } else {
813 _z180_regread_simple(device, offsetwords, value);
814 }
815}
816
817static void z180_regwrite(struct kgsl_device *device,
818 unsigned int offsetwords,
819 unsigned int value)
820{
821 if (!in_interrupt())
822 kgsl_pre_hwaccess(device);
823
824 if ((offsetwords >= MH_ARBITER_CONFIG &&
825 offsetwords <= MH_CLNT_INTF_CTRL_CONFIG2) ||
826 (offsetwords >= MH_MMU_CONFIG &&
827 offsetwords <= MH_MMU_MPU_END)) {
828 _z180_regwrite_mmu(device, offsetwords, value);
829 } else {
830 _z180_regwrite_simple(device, offsetwords, value);
831 }
832}
833
834static void z180_cmdwindow_write(struct kgsl_device *device,
835 unsigned int addr, unsigned int data)
836{
837 unsigned int cmdwinaddr;
838
839 cmdwinaddr = ((Z180_CMDWINDOW_2D << Z180_CMDWINDOW_TARGET_SHIFT) &
840 Z180_CMDWINDOW_TARGET_MASK);
841 cmdwinaddr |= ((addr << Z180_CMDWINDOW_ADDR_SHIFT) &
842 Z180_CMDWINDOW_ADDR_MASK);
843
844 z180_regwrite(device, ADDR_VGC_COMMANDSTREAM >> 2, cmdwinaddr);
845 z180_regwrite(device, ADDR_VGC_COMMANDSTREAM >> 2, data);
846}
847
848static unsigned int z180_readtimestamp(struct kgsl_device *device,
849 struct kgsl_context *context, enum kgsl_timestamp_type type)
850{
851 struct z180_device *z180_dev = Z180_DEVICE(device);
852 (void)context;
853 /* get current EOP timestamp */
854 return z180_dev->timestamp;
855}
856
857static int z180_waittimestamp(struct kgsl_device *device,
858 struct kgsl_context *context,
859 unsigned int timestamp,
860 unsigned int msecs)
861{
862 int status = -EINVAL;
863
864 /* Don't wait forever, set a max of Z180_IDLE_TIMEOUT */
865 if (msecs == -1)
866 msecs = Z180_IDLE_TIMEOUT;
867
Steve Kondik2deaf4b2014-03-20 22:29:09 -0700868 status = kgsl_active_count_get(device);
869 if (!status) {
870 mutex_unlock(&device->mutex);
871 status = z180_wait(device, context, timestamp, msecs);
872 mutex_lock(&device->mutex);
873 kgsl_active_count_put(device);
874 }
Steve Kondikf7652b32013-11-26 15:20:51 -0800875
876 return status;
877}
878
879static int z180_wait(struct kgsl_device *device,
880 struct kgsl_context *context,
881 unsigned int timestamp,
882 unsigned int msecs)
883{
884 int status = -EINVAL;
885 long timeout = 0;
886
887 timeout = wait_io_event_interruptible_timeout(
888 device->wait_queue,
889 kgsl_check_timestamp(device, context, timestamp),
890 msecs_to_jiffies(msecs));
891
892 if (timeout > 0)
893 status = 0;
894 else if (timeout == 0) {
895 status = -ETIMEDOUT;
896 kgsl_pwrctrl_set_state(device, KGSL_STATE_HUNG);
897 kgsl_postmortem_dump(device, 0);
898 } else
899 status = timeout;
900
901 return status;
902}
903
904struct kgsl_context *
905z180_drawctxt_create(struct kgsl_device_private *dev_priv,
906 uint32_t *flags)
907{
908 int ret;
909 struct kgsl_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
910 if (context == NULL)
911 return ERR_PTR(-ENOMEM);
912 ret = kgsl_context_init(dev_priv, context);
913 if (ret != 0) {
914 kfree(context);
915 return ERR_PTR(ret);
916 }
917 return context;
918}
919
920static int
921z180_drawctxt_detach(struct kgsl_context *context)
922{
Steve Kondik2deaf4b2014-03-20 22:29:09 -0700923 int ret;
Steve Kondikf7652b32013-11-26 15:20:51 -0800924 struct kgsl_device *device;
925 struct z180_device *z180_dev;
926
927 device = context->device;
928 z180_dev = Z180_DEVICE(device);
929
Steve Kondik2deaf4b2014-03-20 22:29:09 -0700930 ret = kgsl_active_count_get(device);
931 if (ret)
932 return ret;
933
Steve Kondikf7652b32013-11-26 15:20:51 -0800934 z180_idle(device);
935
936 if (z180_dev->ringbuffer.prevctx == context->id) {
937 z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT;
938 device->mmu.hwpagetable = device->mmu.defaultpagetable;
939
940 /* Ignore the result - we are going down anyway */
941 kgsl_setstate(&device->mmu, KGSL_MEMSTORE_GLOBAL,
942 KGSL_MMUFLAGS_PTUPDATE);
943 }
944
Steve Kondik2deaf4b2014-03-20 22:29:09 -0700945 kgsl_active_count_put(device);
Steve Kondikf7652b32013-11-26 15:20:51 -0800946 return 0;
947}
948
949static void
950z180_drawctxt_destroy(struct kgsl_context *context)
951{
952 kfree(context);
953}
954
955static void z180_power_stats(struct kgsl_device *device,
956 struct kgsl_power_stats *stats)
957{
958 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
959 s64 tmp = ktime_to_us(ktime_get());
960
961 if (pwr->time == 0) {
962 pwr->time = tmp;
963 stats->total_time = 0;
964 stats->busy_time = 0;
965 } else {
966 stats->total_time = tmp - pwr->time;
967 pwr->time = tmp;
968 stats->busy_time = tmp - device->on_time;
969 device->on_time = tmp;
970 }
971}
972
973static void z180_irqctrl(struct kgsl_device *device, int state)
974{
975 /* Control interrupts for Z180 and the Z180 MMU */
976
977 if (state) {
978 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 3);
979 z180_regwrite(device, MH_INTERRUPT_MASK,
980 kgsl_mmu_get_int_mask());
981 } else {
982 z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0);
983 z180_regwrite(device, MH_INTERRUPT_MASK, 0);
984 }
985}
986
987static unsigned int z180_gpuid(struct kgsl_device *device, unsigned int *chipid)
988{
989 if (chipid != NULL)
990 *chipid = 0;
991
992 /* Standard KGSL gpuid format:
993 * top word is 0x0002 for 2D or 0x0003 for 3D
994 * Bottom word is core specific identifer
995 */
996
997 return (0x0002 << 16) | 180;
998}
999
1000static const struct kgsl_functable z180_functable = {
1001 /* Mandatory functions */
1002 .regread = z180_regread,
1003 .regwrite = z180_regwrite,
1004 .idle = z180_idle,
1005 .isidle = z180_isidle,
1006 .suspend_context = z180_suspend_context,
1007 .init = z180_init,
1008 .start = z180_start,
1009 .stop = z180_stop,
1010 .getproperty = z180_getproperty,
1011 .waittimestamp = z180_waittimestamp,
1012 .readtimestamp = z180_readtimestamp,
1013 .issueibcmds = z180_cmdstream_issueibcmds,
1014 .setup_pt = z180_setup_pt,
1015 .cleanup_pt = z180_cleanup_pt,
1016 .power_stats = z180_power_stats,
1017 .irqctrl = z180_irqctrl,
1018 .gpuid = z180_gpuid,
1019 .irq_handler = z180_irq_handler,
1020 .drain = z180_idle, /* drain == idle for the z180 */
1021 /* Optional functions */
1022 .drawctxt_create = z180_drawctxt_create,
1023 .drawctxt_detach = z180_drawctxt_detach,
1024 .drawctxt_destroy = z180_drawctxt_destroy,
1025 .ioctl = NULL,
1026 .postmortem_dump = z180_dump,
1027};
1028
1029static struct platform_device_id z180_id_table[] = {
1030 { DEVICE_2D0_NAME, (kernel_ulong_t)&device_2d0.dev, },
1031 { DEVICE_2D1_NAME, (kernel_ulong_t)&device_2d1.dev, },
1032 { },
1033};
1034MODULE_DEVICE_TABLE(platform, z180_id_table);
1035
1036static struct platform_driver z180_platform_driver = {
1037 .probe = z180_probe,
1038 .remove = __devexit_p(z180_remove),
1039 .suspend = kgsl_suspend_driver,
1040 .resume = kgsl_resume_driver,
1041 .id_table = z180_id_table,
1042 .driver = {
1043 .owner = THIS_MODULE,
1044 .name = DEVICE_2D_NAME,
1045 .pm = &kgsl_pm_ops,
1046 }
1047};
1048
1049static int __init kgsl_2d_init(void)
1050{
1051 return platform_driver_register(&z180_platform_driver);
1052}
1053
1054static void __exit kgsl_2d_exit(void)
1055{
1056 platform_driver_unregister(&z180_platform_driver);
1057}
1058
1059module_init(kgsl_2d_init);
1060module_exit(kgsl_2d_exit);
1061
1062MODULE_DESCRIPTION("2D Graphics driver");
1063MODULE_VERSION("1.2");
1064MODULE_LICENSE("GPL v2");
1065MODULE_ALIAS("platform:kgsl_2d");