Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2002,2007-2011, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | #include <linux/uaccess.h> |
| 14 | |
| 15 | #include "kgsl.h" |
| 16 | #include "kgsl_cffdump.h" |
| 17 | #include "kgsl_sharedmem.h" |
| 18 | |
| 19 | #include "z180.h" |
| 20 | #include "z180_reg.h" |
| 21 | |
| 22 | #define DRIVER_VERSION_MAJOR 3 |
| 23 | #define DRIVER_VERSION_MINOR 1 |
| 24 | |
| 25 | #define Z180_DEVICE(device) \ |
| 26 | KGSL_CONTAINER_OF(device, struct z180_device, dev) |
| 27 | |
| 28 | #define GSL_VGC_INT_MASK \ |
| 29 | (REG_VGC_IRQSTATUS__MH_MASK | \ |
| 30 | REG_VGC_IRQSTATUS__G2D_MASK | \ |
| 31 | REG_VGC_IRQSTATUS__FIFO_MASK) |
| 32 | |
| 33 | #define VGV3_NEXTCMD_JUMP 0x01 |
| 34 | |
| 35 | #define VGV3_NEXTCMD_NEXTCMD_FSHIFT 12 |
| 36 | #define VGV3_NEXTCMD_NEXTCMD_FMASK 0x7 |
| 37 | |
| 38 | #define VGV3_CONTROL_MARKADD_FSHIFT 0 |
| 39 | #define VGV3_CONTROL_MARKADD_FMASK 0xfff |
| 40 | |
| 41 | #define Z180_PACKET_SIZE 15 |
| 42 | #define Z180_MARKER_SIZE 10 |
| 43 | #define Z180_CALL_CMD 0x1000 |
| 44 | #define Z180_MARKER_CMD 0x8000 |
| 45 | #define Z180_STREAM_END_CMD 0x9000 |
| 46 | #define Z180_STREAM_PACKET 0x7C000176 |
| 47 | #define Z180_STREAM_PACKET_CALL 0x7C000275 |
| 48 | #define Z180_PACKET_COUNT 8 |
| 49 | #define Z180_RB_SIZE (Z180_PACKET_SIZE*Z180_PACKET_COUNT \ |
| 50 | *sizeof(uint32_t)) |
| 51 | |
| 52 | #define NUMTEXUNITS 4 |
| 53 | #define TEXUNITREGCOUNT 25 |
| 54 | #define VG_REGCOUNT 0x39 |
| 55 | |
| 56 | #define PACKETSIZE_BEGIN 3 |
| 57 | #define PACKETSIZE_G2DCOLOR 2 |
| 58 | #define PACKETSIZE_TEXUNIT (TEXUNITREGCOUNT * 2) |
| 59 | #define PACKETSIZE_REG (VG_REGCOUNT * 2) |
| 60 | #define PACKETSIZE_STATE (PACKETSIZE_TEXUNIT * NUMTEXUNITS + \ |
| 61 | PACKETSIZE_REG + PACKETSIZE_BEGIN + \ |
| 62 | PACKETSIZE_G2DCOLOR) |
| 63 | #define PACKETSIZE_STATESTREAM (ALIGN((PACKETSIZE_STATE * \ |
| 64 | sizeof(unsigned int)), 32) / \ |
| 65 | sizeof(unsigned int)) |
| 66 | |
| 67 | #define Z180_INVALID_CONTEXT UINT_MAX |
| 68 | |
| 69 | /* z180 MH arbiter config*/ |
| 70 | #define Z180_CFG_MHARB \ |
| 71 | (0x10 \ |
| 72 | | (0 << MH_ARBITER_CONFIG__SAME_PAGE_GRANULARITY__SHIFT) \ |
| 73 | | (1 << MH_ARBITER_CONFIG__L1_ARB_ENABLE__SHIFT) \ |
| 74 | | (1 << MH_ARBITER_CONFIG__L1_ARB_HOLD_ENABLE__SHIFT) \ |
| 75 | | (0 << MH_ARBITER_CONFIG__L2_ARB_CONTROL__SHIFT) \ |
| 76 | | (1 << MH_ARBITER_CONFIG__PAGE_SIZE__SHIFT) \ |
| 77 | | (1 << MH_ARBITER_CONFIG__TC_REORDER_ENABLE__SHIFT) \ |
| 78 | | (1 << MH_ARBITER_CONFIG__TC_ARB_HOLD_ENABLE__SHIFT) \ |
| 79 | | (0 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT_ENABLE__SHIFT) \ |
| 80 | | (0x8 << MH_ARBITER_CONFIG__IN_FLIGHT_LIMIT__SHIFT) \ |
| 81 | | (1 << MH_ARBITER_CONFIG__CP_CLNT_ENABLE__SHIFT) \ |
| 82 | | (1 << MH_ARBITER_CONFIG__VGT_CLNT_ENABLE__SHIFT) \ |
| 83 | | (1 << MH_ARBITER_CONFIG__TC_CLNT_ENABLE__SHIFT) \ |
| 84 | | (1 << MH_ARBITER_CONFIG__RB_CLNT_ENABLE__SHIFT) \ |
| 85 | | (1 << MH_ARBITER_CONFIG__PA_CLNT_ENABLE__SHIFT)) |
| 86 | |
| 87 | #define Z180_TIMESTAMP_EPSILON 20000 |
| 88 | #define Z180_IDLE_COUNT_MAX 1000000 |
| 89 | |
| 90 | enum z180_cmdwindow_type { |
| 91 | Z180_CMDWINDOW_2D = 0x00000000, |
| 92 | Z180_CMDWINDOW_MMU = 0x00000002, |
| 93 | }; |
| 94 | |
| 95 | #define Z180_CMDWINDOW_TARGET_MASK 0x000000FF |
| 96 | #define Z180_CMDWINDOW_ADDR_MASK 0x00FFFF00 |
| 97 | #define Z180_CMDWINDOW_TARGET_SHIFT 0 |
| 98 | #define Z180_CMDWINDOW_ADDR_SHIFT 8 |
| 99 | |
| 100 | static int z180_start(struct kgsl_device *device, unsigned int init_ram); |
| 101 | static int z180_stop(struct kgsl_device *device); |
| 102 | static int z180_wait(struct kgsl_device *device, |
| 103 | unsigned int timestamp, |
| 104 | unsigned int msecs); |
| 105 | static void z180_regread(struct kgsl_device *device, |
| 106 | unsigned int offsetwords, |
| 107 | unsigned int *value); |
| 108 | static void z180_regwrite(struct kgsl_device *device, |
| 109 | unsigned int offsetwords, |
| 110 | unsigned int value); |
| 111 | static void z180_cmdwindow_write(struct kgsl_device *device, |
| 112 | unsigned int addr, |
| 113 | unsigned int data); |
| 114 | |
| 115 | #define Z180_MMU_CONFIG \ |
| 116 | (0x01 \ |
| 117 | | (MMU_CONFIG << MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT) \ |
| 118 | | (MMU_CONFIG << MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT) \ |
| 119 | | (MMU_CONFIG << MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT) \ |
| 120 | | (MMU_CONFIG << MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT) \ |
| 121 | | (MMU_CONFIG << MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT) \ |
| 122 | | (MMU_CONFIG << MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT) \ |
| 123 | | (MMU_CONFIG << MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT) \ |
| 124 | | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT) \ |
| 125 | | (MMU_CONFIG << MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT) \ |
| 126 | | (MMU_CONFIG << MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT) \ |
| 127 | | (MMU_CONFIG << MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT)) |
| 128 | |
| 129 | static const struct kgsl_functable z180_functable; |
| 130 | |
| 131 | static struct z180_device device_2d0 = { |
| 132 | .dev = { |
| 133 | .name = DEVICE_2D0_NAME, |
| 134 | .id = KGSL_DEVICE_2D0, |
| 135 | .ver_major = DRIVER_VERSION_MAJOR, |
| 136 | .ver_minor = DRIVER_VERSION_MINOR, |
| 137 | .mmu = { |
| 138 | .config = Z180_MMU_CONFIG, |
| 139 | /* turn off memory protection unit by setting |
| 140 | acceptable physical address range to include |
| 141 | all pages. */ |
| 142 | .mpu_base = 0x00000000, |
| 143 | .mpu_range = 0xFFFFF000, |
| 144 | }, |
| 145 | .pwrctrl = { |
| 146 | .regulator_name = "fs_gfx2d0", |
| 147 | .irq_name = KGSL_2D0_IRQ, |
| 148 | }, |
| 149 | .mutex = __MUTEX_INITIALIZER(device_2d0.dev.mutex), |
| 150 | .state = KGSL_STATE_INIT, |
| 151 | .active_cnt = 0, |
| 152 | .iomemname = KGSL_2D0_REG_MEMORY, |
| 153 | .ftbl = &z180_functable, |
| 154 | .display_off = { |
| 155 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 156 | .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING, |
| 157 | .suspend = kgsl_early_suspend_driver, |
| 158 | .resume = kgsl_late_resume_driver, |
| 159 | #endif |
| 160 | }, |
| 161 | }, |
| 162 | }; |
| 163 | |
| 164 | static struct z180_device device_2d1 = { |
| 165 | .dev = { |
| 166 | .name = DEVICE_2D1_NAME, |
| 167 | .id = KGSL_DEVICE_2D1, |
| 168 | .ver_major = DRIVER_VERSION_MAJOR, |
| 169 | .ver_minor = DRIVER_VERSION_MINOR, |
| 170 | .mmu = { |
| 171 | .config = Z180_MMU_CONFIG, |
| 172 | /* turn off memory protection unit by setting |
| 173 | acceptable physical address range to include |
| 174 | all pages. */ |
| 175 | .mpu_base = 0x00000000, |
| 176 | .mpu_range = 0xFFFFF000, |
| 177 | }, |
| 178 | .pwrctrl = { |
| 179 | .regulator_name = "fs_gfx2d1", |
| 180 | .irq_name = KGSL_2D1_IRQ, |
| 181 | }, |
| 182 | .mutex = __MUTEX_INITIALIZER(device_2d1.dev.mutex), |
| 183 | .state = KGSL_STATE_INIT, |
| 184 | .active_cnt = 0, |
| 185 | .iomemname = KGSL_2D1_REG_MEMORY, |
| 186 | .ftbl = &z180_functable, |
| 187 | .display_off = { |
| 188 | #ifdef CONFIG_HAS_EARLYSUSPEND |
| 189 | .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING, |
| 190 | .suspend = kgsl_early_suspend_driver, |
| 191 | .resume = kgsl_late_resume_driver, |
| 192 | #endif |
| 193 | }, |
| 194 | }, |
| 195 | }; |
| 196 | |
| 197 | static irqreturn_t z180_isr(int irq, void *data) |
| 198 | { |
| 199 | irqreturn_t result = IRQ_NONE; |
| 200 | unsigned int status; |
| 201 | struct kgsl_device *device = (struct kgsl_device *) data; |
| 202 | struct z180_device *z180_dev = Z180_DEVICE(device); |
| 203 | |
| 204 | z180_regread(device, ADDR_VGC_IRQSTATUS >> 2, &status); |
| 205 | |
| 206 | if (status & GSL_VGC_INT_MASK) { |
| 207 | z180_regwrite(device, |
| 208 | ADDR_VGC_IRQSTATUS >> 2, status & GSL_VGC_INT_MASK); |
| 209 | |
| 210 | result = IRQ_HANDLED; |
| 211 | |
| 212 | if (status & REG_VGC_IRQSTATUS__FIFO_MASK) |
| 213 | KGSL_DRV_ERR(device, "z180 fifo interrupt\n"); |
| 214 | if (status & REG_VGC_IRQSTATUS__MH_MASK) |
| 215 | kgsl_mh_intrcallback(device); |
| 216 | if (status & REG_VGC_IRQSTATUS__G2D_MASK) { |
| 217 | int count; |
| 218 | |
| 219 | z180_regread(device, |
| 220 | ADDR_VGC_IRQ_ACTIVE_CNT >> 2, |
| 221 | &count); |
| 222 | |
| 223 | count >>= 8; |
| 224 | count &= 255; |
| 225 | z180_dev->timestamp += count; |
| 226 | |
| 227 | wake_up_interruptible(&device->wait_queue); |
| 228 | |
| 229 | atomic_notifier_call_chain( |
| 230 | &(device->ts_notifier_list), |
| 231 | device->id, NULL); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if ((device->pwrctrl.nap_allowed == true) && |
| 236 | (device->requested_state == KGSL_STATE_NONE)) { |
| 237 | device->requested_state = KGSL_STATE_NAP; |
| 238 | queue_work(device->work_queue, &device->idle_check_ws); |
| 239 | } |
| 240 | mod_timer(&device->idle_timer, |
| 241 | jiffies + device->pwrctrl.interval_timeout); |
| 242 | |
| 243 | return result; |
| 244 | } |
| 245 | |
| 246 | static int z180_cleanup_pt(struct kgsl_device *device, |
| 247 | struct kgsl_pagetable *pagetable) |
| 248 | { |
| 249 | struct z180_device *z180_dev = Z180_DEVICE(device); |
| 250 | |
| 251 | kgsl_mmu_unmap(pagetable, &device->mmu.dummyspace); |
| 252 | |
| 253 | kgsl_mmu_unmap(pagetable, &device->memstore); |
| 254 | |
| 255 | kgsl_mmu_unmap(pagetable, &z180_dev->ringbuffer.cmdbufdesc); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int z180_setup_pt(struct kgsl_device *device, |
| 261 | struct kgsl_pagetable *pagetable) |
| 262 | { |
| 263 | int result = 0; |
| 264 | struct z180_device *z180_dev = Z180_DEVICE(device); |
| 265 | |
| 266 | result = kgsl_mmu_map_global(pagetable, &device->mmu.dummyspace, |
| 267 | GSL_PT_PAGE_RV | GSL_PT_PAGE_WV); |
| 268 | |
| 269 | if (result) |
| 270 | goto error; |
| 271 | |
| 272 | result = kgsl_mmu_map_global(pagetable, &device->memstore, |
| 273 | GSL_PT_PAGE_RV | GSL_PT_PAGE_WV); |
| 274 | if (result) |
| 275 | goto error_unmap_dummy; |
| 276 | |
| 277 | result = kgsl_mmu_map_global(pagetable, |
| 278 | &z180_dev->ringbuffer.cmdbufdesc, |
| 279 | GSL_PT_PAGE_RV); |
| 280 | if (result) |
| 281 | goto error_unmap_memstore; |
| 282 | return result; |
| 283 | |
| 284 | error_unmap_dummy: |
| 285 | kgsl_mmu_unmap(pagetable, &device->mmu.dummyspace); |
| 286 | |
| 287 | error_unmap_memstore: |
| 288 | kgsl_mmu_unmap(pagetable, &device->memstore); |
| 289 | |
| 290 | error: |
| 291 | return result; |
| 292 | } |
| 293 | |
| 294 | static inline unsigned int rb_offset(unsigned int index) |
| 295 | { |
| 296 | return index*sizeof(unsigned int)*(Z180_PACKET_SIZE); |
| 297 | } |
| 298 | |
| 299 | static void addmarker(struct z180_ringbuffer *rb, unsigned int index) |
| 300 | { |
| 301 | char *ptr = (char *)(rb->cmdbufdesc.hostptr); |
| 302 | unsigned int *p = (unsigned int *)(ptr + rb_offset(index)); |
| 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 | |
| 316 | static void addcmd(struct z180_ringbuffer *rb, unsigned int index, |
| 317 | unsigned int cmd, unsigned int nextcnt) |
| 318 | { |
| 319 | char * ptr = (char *)(rb->cmdbufdesc.hostptr); |
| 320 | unsigned int *p = (unsigned int *)(ptr + (rb_offset(index) |
| 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 | |
| 330 | static 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 | z180_dev->timestamp = 0; |
| 336 | z180_dev->current_timestamp = 0; |
| 337 | |
| 338 | addmarker(&z180_dev->ringbuffer, 0); |
| 339 | |
| 340 | z180_cmdwindow_write(device, ADDR_VGV3_MODE, 4); |
| 341 | |
| 342 | z180_cmdwindow_write(device, ADDR_VGV3_NEXTADDR, |
| 343 | z180_dev->ringbuffer.cmdbufdesc.gpuaddr); |
| 344 | |
| 345 | z180_cmdwindow_write(device, ADDR_VGV3_NEXTCMD, cmd | 5); |
| 346 | |
| 347 | z180_cmdwindow_write(device, ADDR_VGV3_WRITEADDR, |
| 348 | device->memstore.gpuaddr); |
| 349 | |
| 350 | cmd = (int)(((1) & VGV3_CONTROL_MARKADD_FMASK) |
| 351 | << VGV3_CONTROL_MARKADD_FSHIFT); |
| 352 | |
| 353 | z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, cmd); |
| 354 | |
| 355 | z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, 0); |
| 356 | } |
| 357 | |
| 358 | static int room_in_rb(struct z180_device *device) |
| 359 | { |
| 360 | int ts_diff; |
| 361 | |
| 362 | ts_diff = device->current_timestamp - device->timestamp; |
| 363 | |
| 364 | return ts_diff < Z180_PACKET_COUNT; |
| 365 | } |
| 366 | |
| 367 | static int z180_idle(struct kgsl_device *device, unsigned int timeout) |
| 368 | { |
| 369 | int status = 0; |
| 370 | struct z180_device *z180_dev = Z180_DEVICE(device); |
| 371 | |
| 372 | if (z180_dev->current_timestamp > z180_dev->timestamp) |
| 373 | status = z180_wait(device, z180_dev->current_timestamp, |
| 374 | timeout); |
| 375 | |
| 376 | if (status) |
| 377 | KGSL_DRV_ERR(device, "z180_waittimestamp() timed out\n"); |
| 378 | |
| 379 | return status; |
| 380 | } |
| 381 | |
| 382 | static void z180_setstate(struct kgsl_device *device, uint32_t flags) |
| 383 | { |
| 384 | kgsl_default_setstate(device, flags); |
| 385 | } |
| 386 | |
| 387 | int |
| 388 | z180_cmdstream_issueibcmds(struct kgsl_device_private *dev_priv, |
| 389 | struct kgsl_context *context, |
| 390 | struct kgsl_ibdesc *ibdesc, |
| 391 | unsigned int numibs, |
| 392 | uint32_t *timestamp, |
| 393 | unsigned int ctrl) |
| 394 | { |
| 395 | unsigned int result = 0; |
| 396 | unsigned int ofs = PACKETSIZE_STATESTREAM * sizeof(unsigned int); |
| 397 | unsigned int cnt = 5; |
| 398 | unsigned int nextaddr = 0; |
| 399 | unsigned int index = 0; |
| 400 | unsigned int nextindex; |
| 401 | unsigned int nextcnt = Z180_STREAM_END_CMD | 5; |
| 402 | struct kgsl_memdesc tmp = {0}; |
| 403 | unsigned int cmd; |
| 404 | struct kgsl_device *device = dev_priv->device; |
| 405 | struct kgsl_pagetable *pagetable = dev_priv->process_priv->pagetable; |
| 406 | struct z180_device *z180_dev = Z180_DEVICE(device); |
| 407 | unsigned int sizedwords; |
| 408 | |
| 409 | if (device->state & KGSL_STATE_HUNG) { |
| 410 | return -EINVAL; |
| 411 | goto error; |
| 412 | } |
| 413 | if (numibs != 1) { |
| 414 | KGSL_DRV_ERR(device, "Invalid number of ibs: %d\n", numibs); |
| 415 | result = -EINVAL; |
| 416 | goto error; |
| 417 | } |
| 418 | cmd = ibdesc[0].gpuaddr; |
| 419 | sizedwords = ibdesc[0].sizedwords; |
| 420 | |
| 421 | tmp.hostptr = (void *)*timestamp; |
| 422 | |
| 423 | KGSL_CMD_INFO(device, "ctxt %d ibaddr 0x%08x sizedwords %d\n", |
| 424 | context->id, cmd, sizedwords); |
| 425 | /* context switch */ |
| 426 | if ((context->id != (int)z180_dev->ringbuffer.prevctx) || |
| 427 | (ctrl & KGSL_CONTEXT_CTX_SWITCH)) { |
| 428 | KGSL_CMD_INFO(device, "context switch %d -> %d\n", |
| 429 | context->id, z180_dev->ringbuffer.prevctx); |
| 430 | kgsl_mmu_setstate(device, pagetable); |
| 431 | cnt = PACKETSIZE_STATESTREAM; |
| 432 | ofs = 0; |
| 433 | } |
| 434 | z180_setstate(device, kgsl_pt_get_flags(device->mmu.hwpagetable, |
| 435 | device->id)); |
| 436 | |
| 437 | result = wait_event_interruptible_timeout(device->wait_queue, |
| 438 | room_in_rb(z180_dev), |
| 439 | msecs_to_jiffies(KGSL_TIMEOUT_DEFAULT)); |
| 440 | if (result < 0) { |
| 441 | KGSL_CMD_ERR(device, "wait_event_interruptible_timeout " |
| 442 | "failed: %d\n", result); |
| 443 | goto error; |
| 444 | } |
| 445 | result = 0; |
| 446 | |
| 447 | index = z180_dev->current_timestamp % Z180_PACKET_COUNT; |
| 448 | z180_dev->current_timestamp++; |
| 449 | nextindex = z180_dev->current_timestamp % Z180_PACKET_COUNT; |
| 450 | *timestamp = z180_dev->current_timestamp; |
| 451 | |
| 452 | z180_dev->ringbuffer.prevctx = context->id; |
| 453 | |
| 454 | addcmd(&z180_dev->ringbuffer, index, cmd + ofs, cnt); |
| 455 | |
| 456 | /* Make sure the next ringbuffer entry has a marker */ |
| 457 | addmarker(&z180_dev->ringbuffer, nextindex); |
| 458 | |
| 459 | nextaddr = z180_dev->ringbuffer.cmdbufdesc.gpuaddr |
| 460 | + rb_offset(nextindex); |
| 461 | |
| 462 | tmp.hostptr = (void *)(tmp.hostptr + |
| 463 | (sizedwords * sizeof(unsigned int))); |
| 464 | tmp.size = 12; |
| 465 | |
| 466 | kgsl_sharedmem_writel(&tmp, 4, nextaddr); |
| 467 | kgsl_sharedmem_writel(&tmp, 8, nextcnt); |
| 468 | |
| 469 | /* sync memory before activating the hardware for the new command*/ |
| 470 | mb(); |
| 471 | |
| 472 | cmd = (int)(((2) & VGV3_CONTROL_MARKADD_FMASK) |
| 473 | << VGV3_CONTROL_MARKADD_FSHIFT); |
| 474 | |
| 475 | z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, cmd); |
| 476 | z180_cmdwindow_write(device, ADDR_VGV3_CONTROL, 0); |
| 477 | error: |
| 478 | return result; |
| 479 | } |
| 480 | |
| 481 | static int z180_ringbuffer_init(struct kgsl_device *device) |
| 482 | { |
| 483 | struct z180_device *z180_dev = Z180_DEVICE(device); |
| 484 | memset(&z180_dev->ringbuffer, 0, sizeof(struct z180_ringbuffer)); |
| 485 | z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT; |
| 486 | return kgsl_allocate_contiguous(&z180_dev->ringbuffer.cmdbufdesc, |
| 487 | Z180_RB_SIZE); |
| 488 | } |
| 489 | |
| 490 | static void z180_ringbuffer_close(struct kgsl_device *device) |
| 491 | { |
| 492 | struct z180_device *z180_dev = Z180_DEVICE(device); |
| 493 | kgsl_sharedmem_free(&z180_dev->ringbuffer.cmdbufdesc); |
| 494 | memset(&z180_dev->ringbuffer, 0, sizeof(struct z180_ringbuffer)); |
| 495 | } |
| 496 | |
| 497 | static int __devinit z180_probe(struct platform_device *pdev) |
| 498 | { |
| 499 | int status = -EINVAL; |
| 500 | struct kgsl_device *device = NULL; |
| 501 | struct z180_device *z180_dev; |
| 502 | |
| 503 | device = (struct kgsl_device *)pdev->id_entry->driver_data; |
| 504 | device->parentdev = &pdev->dev; |
| 505 | |
| 506 | z180_dev = Z180_DEVICE(device); |
| 507 | spin_lock_init(&z180_dev->cmdwin_lock); |
| 508 | |
| 509 | status = z180_ringbuffer_init(device); |
| 510 | if (status != 0) |
| 511 | goto error; |
| 512 | |
| 513 | status = kgsl_device_platform_probe(device, z180_isr); |
| 514 | if (status) |
| 515 | goto error_close_ringbuffer; |
| 516 | |
| 517 | return status; |
| 518 | |
| 519 | error_close_ringbuffer: |
| 520 | z180_ringbuffer_close(device); |
| 521 | error: |
| 522 | device->parentdev = NULL; |
| 523 | return status; |
| 524 | } |
| 525 | |
| 526 | static int __devexit z180_remove(struct platform_device *pdev) |
| 527 | { |
| 528 | struct kgsl_device *device = NULL; |
| 529 | |
| 530 | device = (struct kgsl_device *)pdev->id_entry->driver_data; |
| 531 | |
| 532 | kgsl_device_platform_remove(device); |
| 533 | |
| 534 | z180_ringbuffer_close(device); |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | static int z180_start(struct kgsl_device *device, unsigned int init_ram) |
| 540 | { |
| 541 | int status = 0; |
| 542 | |
| 543 | device->state = KGSL_STATE_INIT; |
| 544 | device->requested_state = KGSL_STATE_NONE; |
| 545 | KGSL_PWR_WARN(device, "state -> INIT, device %d\n", device->id); |
| 546 | |
| 547 | kgsl_pwrctrl_enable(device); |
| 548 | |
| 549 | /* Set up MH arbiter. MH offsets are considered to be dword |
| 550 | * based, therefore no down shift. */ |
| 551 | z180_regwrite(device, ADDR_MH_ARBITER_CONFIG, Z180_CFG_MHARB); |
| 552 | |
| 553 | z180_regwrite(device, ADDR_MH_CLNT_INTF_CTRL_CONFIG1, 0x00030F27); |
| 554 | z180_regwrite(device, ADDR_MH_CLNT_INTF_CTRL_CONFIG2, 0x004B274F); |
| 555 | |
| 556 | /* Set interrupts to 0 to ensure a good state */ |
| 557 | z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0x0); |
| 558 | |
| 559 | status = kgsl_mmu_start(device); |
| 560 | if (status) |
| 561 | goto error_clk_off; |
| 562 | |
| 563 | z180_cmdstream_start(device); |
| 564 | |
| 565 | mod_timer(&device->idle_timer, jiffies + FIRST_TIMEOUT); |
| 566 | kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_IRQ_ON); |
| 567 | return 0; |
| 568 | |
| 569 | error_clk_off: |
| 570 | z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0); |
| 571 | kgsl_pwrctrl_disable(device); |
| 572 | return status; |
| 573 | } |
| 574 | |
| 575 | static int z180_stop(struct kgsl_device *device) |
| 576 | { |
| 577 | z180_idle(device, KGSL_TIMEOUT_DEFAULT); |
| 578 | |
Jeremy Gebben | 1757a85 | 2011-07-11 16:04:38 -0600 | [diff] [blame^] | 579 | del_timer_sync(&device->idle_timer); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 580 | |
| 581 | kgsl_mmu_stop(device); |
| 582 | |
| 583 | /* Disable the clocks before the power rail. */ |
| 584 | kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF); |
| 585 | |
| 586 | kgsl_pwrctrl_disable(device); |
| 587 | |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | static int z180_getproperty(struct kgsl_device *device, |
| 592 | enum kgsl_property_type type, |
| 593 | void *value, |
| 594 | unsigned int sizebytes) |
| 595 | { |
| 596 | int status = -EINVAL; |
| 597 | |
| 598 | switch (type) { |
| 599 | case KGSL_PROP_DEVICE_INFO: |
| 600 | { |
| 601 | struct kgsl_devinfo devinfo; |
| 602 | |
| 603 | if (sizebytes != sizeof(devinfo)) { |
| 604 | status = -EINVAL; |
| 605 | break; |
| 606 | } |
| 607 | |
| 608 | memset(&devinfo, 0, sizeof(devinfo)); |
| 609 | devinfo.device_id = device->id+1; |
| 610 | devinfo.chip_id = 0; |
| 611 | devinfo.mmu_enabled = kgsl_mmu_enabled(); |
| 612 | |
| 613 | if (copy_to_user(value, &devinfo, sizeof(devinfo)) != |
| 614 | 0) { |
| 615 | status = -EFAULT; |
| 616 | break; |
| 617 | } |
| 618 | status = 0; |
| 619 | } |
| 620 | break; |
| 621 | case KGSL_PROP_MMU_ENABLE: |
| 622 | { |
| 623 | #ifdef CONFIG_MSM_KGSL_MMU |
| 624 | int mmuProp = 1; |
| 625 | #else |
| 626 | int mmuProp = 0; |
| 627 | #endif |
| 628 | if (sizebytes != sizeof(int)) { |
| 629 | status = -EINVAL; |
| 630 | break; |
| 631 | } |
| 632 | if (copy_to_user(value, &mmuProp, sizeof(mmuProp))) { |
| 633 | status = -EFAULT; |
| 634 | break; |
| 635 | } |
| 636 | status = 0; |
| 637 | } |
| 638 | break; |
| 639 | |
| 640 | default: |
| 641 | KGSL_DRV_ERR(device, "invalid property: %d\n", type); |
| 642 | status = -EINVAL; |
| 643 | } |
| 644 | return status; |
| 645 | } |
| 646 | |
| 647 | static unsigned int z180_isidle(struct kgsl_device *device) |
| 648 | { |
| 649 | int status = false; |
| 650 | struct z180_device *z180_dev = Z180_DEVICE(device); |
| 651 | |
| 652 | int timestamp = z180_dev->timestamp; |
| 653 | |
| 654 | if (timestamp == z180_dev->current_timestamp) |
| 655 | status = true; |
| 656 | |
| 657 | return status; |
| 658 | } |
| 659 | |
| 660 | static int z180_suspend_context(struct kgsl_device *device) |
| 661 | { |
| 662 | struct z180_device *z180_dev = Z180_DEVICE(device); |
| 663 | |
| 664 | z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT; |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | /* Not all Z180 registers are directly accessible. |
| 670 | * The _z180_(read|write)_simple functions below handle the ones that are. |
| 671 | */ |
| 672 | static void _z180_regread_simple(struct kgsl_device *device, |
| 673 | unsigned int offsetwords, |
| 674 | unsigned int *value) |
| 675 | { |
| 676 | unsigned int *reg; |
| 677 | |
| 678 | BUG_ON(offsetwords * sizeof(uint32_t) >= device->regspace.sizebytes); |
| 679 | |
| 680 | reg = (unsigned int *)(device->regspace.mmio_virt_base |
| 681 | + (offsetwords << 2)); |
| 682 | |
| 683 | /*ensure this read finishes before the next one. |
| 684 | * i.e. act like normal readl() */ |
| 685 | *value = __raw_readl(reg); |
| 686 | rmb(); |
| 687 | |
| 688 | } |
| 689 | |
| 690 | static void _z180_regwrite_simple(struct kgsl_device *device, |
| 691 | unsigned int offsetwords, |
| 692 | unsigned int value) |
| 693 | { |
| 694 | unsigned int *reg; |
| 695 | |
| 696 | BUG_ON(offsetwords*sizeof(uint32_t) >= device->regspace.sizebytes); |
| 697 | |
| 698 | reg = (unsigned int *)(device->regspace.mmio_virt_base |
| 699 | + (offsetwords << 2)); |
| 700 | kgsl_cffdump_regwrite(device->id, offsetwords << 2, value); |
| 701 | /*ensure previous writes post before this one, |
| 702 | * i.e. act like normal writel() */ |
| 703 | wmb(); |
| 704 | __raw_writel(value, reg); |
| 705 | } |
| 706 | |
| 707 | |
| 708 | /* The MH registers must be accessed through via a 2 step write, (read|write) |
| 709 | * process. These registers may be accessed from interrupt context during |
| 710 | * the handling of MH or MMU error interrupts. Therefore a spin lock is used |
| 711 | * to ensure that the 2 step sequence is not interrupted. |
| 712 | */ |
| 713 | static void _z180_regread_mmu(struct kgsl_device *device, |
| 714 | unsigned int offsetwords, |
| 715 | unsigned int *value) |
| 716 | { |
| 717 | struct z180_device *z180_dev = Z180_DEVICE(device); |
| 718 | unsigned long flags; |
| 719 | |
| 720 | spin_lock_irqsave(&z180_dev->cmdwin_lock, flags); |
| 721 | _z180_regwrite_simple(device, (ADDR_VGC_MH_READ_ADDR >> 2), |
| 722 | offsetwords); |
| 723 | _z180_regread_simple(device, (ADDR_VGC_MH_DATA_ADDR >> 2), value); |
| 724 | spin_unlock_irqrestore(&z180_dev->cmdwin_lock, flags); |
| 725 | } |
| 726 | |
| 727 | |
| 728 | static void _z180_regwrite_mmu(struct kgsl_device *device, |
| 729 | unsigned int offsetwords, |
| 730 | unsigned int value) |
| 731 | { |
| 732 | struct z180_device *z180_dev = Z180_DEVICE(device); |
| 733 | unsigned int cmdwinaddr; |
| 734 | unsigned long flags; |
| 735 | |
| 736 | cmdwinaddr = ((Z180_CMDWINDOW_MMU << Z180_CMDWINDOW_TARGET_SHIFT) & |
| 737 | Z180_CMDWINDOW_TARGET_MASK); |
| 738 | cmdwinaddr |= ((offsetwords << Z180_CMDWINDOW_ADDR_SHIFT) & |
| 739 | Z180_CMDWINDOW_ADDR_MASK); |
| 740 | |
| 741 | spin_lock_irqsave(&z180_dev->cmdwin_lock, flags); |
| 742 | _z180_regwrite_simple(device, ADDR_VGC_MMUCOMMANDSTREAM >> 2, |
| 743 | cmdwinaddr); |
| 744 | _z180_regwrite_simple(device, ADDR_VGC_MMUCOMMANDSTREAM >> 2, value); |
| 745 | spin_unlock_irqrestore(&z180_dev->cmdwin_lock, flags); |
| 746 | } |
| 747 | |
| 748 | /* the rest of the code doesn't want to think about if it is writing mmu |
| 749 | * registers or normal registers so handle it here |
| 750 | */ |
| 751 | static void z180_regread(struct kgsl_device *device, |
| 752 | unsigned int offsetwords, |
| 753 | unsigned int *value) |
| 754 | { |
| 755 | if (!in_interrupt()) |
| 756 | kgsl_pre_hwaccess(device); |
| 757 | |
| 758 | if ((offsetwords >= ADDR_MH_ARBITER_CONFIG && |
| 759 | offsetwords <= ADDR_MH_AXI_HALT_CONTROL) || |
| 760 | (offsetwords >= MH_MMU_CONFIG && |
| 761 | offsetwords <= MH_MMU_MPU_END)) { |
| 762 | _z180_regread_mmu(device, offsetwords, value); |
| 763 | } else { |
| 764 | _z180_regread_simple(device, offsetwords, value); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | static void z180_regwrite(struct kgsl_device *device, |
| 769 | unsigned int offsetwords, |
| 770 | unsigned int value) |
| 771 | { |
| 772 | if (!in_interrupt()) |
| 773 | kgsl_pre_hwaccess(device); |
| 774 | |
| 775 | if ((offsetwords >= ADDR_MH_ARBITER_CONFIG && |
| 776 | offsetwords <= ADDR_MH_CLNT_INTF_CTRL_CONFIG2) || |
| 777 | (offsetwords >= MH_MMU_CONFIG && |
| 778 | offsetwords <= MH_MMU_MPU_END)) { |
| 779 | _z180_regwrite_mmu(device, offsetwords, value); |
| 780 | } else { |
| 781 | _z180_regwrite_simple(device, offsetwords, value); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | static void z180_cmdwindow_write(struct kgsl_device *device, |
| 786 | unsigned int addr, unsigned int data) |
| 787 | { |
| 788 | unsigned int cmdwinaddr; |
| 789 | |
| 790 | cmdwinaddr = ((Z180_CMDWINDOW_2D << Z180_CMDWINDOW_TARGET_SHIFT) & |
| 791 | Z180_CMDWINDOW_TARGET_MASK); |
| 792 | cmdwinaddr |= ((addr << Z180_CMDWINDOW_ADDR_SHIFT) & |
| 793 | Z180_CMDWINDOW_ADDR_MASK); |
| 794 | |
| 795 | z180_regwrite(device, ADDR_VGC_COMMANDSTREAM >> 2, cmdwinaddr); |
| 796 | z180_regwrite(device, ADDR_VGC_COMMANDSTREAM >> 2, data); |
| 797 | } |
| 798 | |
| 799 | static unsigned int z180_readtimestamp(struct kgsl_device *device, |
| 800 | enum kgsl_timestamp_type type) |
| 801 | { |
| 802 | struct z180_device *z180_dev = Z180_DEVICE(device); |
| 803 | /* get current EOP timestamp */ |
| 804 | return z180_dev->timestamp; |
| 805 | } |
| 806 | |
| 807 | static int z180_waittimestamp(struct kgsl_device *device, |
| 808 | unsigned int timestamp, |
| 809 | unsigned int msecs) |
| 810 | { |
| 811 | int status = -EINVAL; |
| 812 | mutex_unlock(&device->mutex); |
| 813 | status = z180_wait(device, timestamp, msecs); |
| 814 | mutex_lock(&device->mutex); |
| 815 | |
| 816 | return status; |
| 817 | } |
| 818 | |
| 819 | static int z180_wait(struct kgsl_device *device, |
| 820 | unsigned int timestamp, |
| 821 | unsigned int msecs) |
| 822 | { |
| 823 | int status = -EINVAL; |
| 824 | long timeout = 0; |
| 825 | |
| 826 | timeout = wait_io_event_interruptible_timeout( |
| 827 | device->wait_queue, |
| 828 | kgsl_check_timestamp(device, timestamp), |
| 829 | msecs_to_jiffies(msecs)); |
| 830 | |
| 831 | if (timeout > 0) |
| 832 | status = 0; |
| 833 | else if (timeout == 0) { |
| 834 | status = -ETIMEDOUT; |
| 835 | device->state = KGSL_STATE_HUNG; |
| 836 | KGSL_PWR_WARN(device, "state -> HUNG, device %d\n", device->id); |
| 837 | } else |
| 838 | status = timeout; |
| 839 | |
| 840 | return status; |
| 841 | } |
| 842 | |
| 843 | static void |
| 844 | z180_drawctxt_destroy(struct kgsl_device *device, |
| 845 | struct kgsl_context *context) |
| 846 | { |
| 847 | struct z180_device *z180_dev = Z180_DEVICE(device); |
| 848 | |
| 849 | z180_idle(device, KGSL_TIMEOUT_DEFAULT); |
| 850 | |
| 851 | if (z180_dev->ringbuffer.prevctx == context->id) { |
| 852 | z180_dev->ringbuffer.prevctx = Z180_INVALID_CONTEXT; |
| 853 | device->mmu.hwpagetable = device->mmu.defaultpagetable; |
| 854 | kgsl_setstate(device, KGSL_MMUFLAGS_PTUPDATE); |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | static void z180_power_stats(struct kgsl_device *device, |
| 859 | struct kgsl_power_stats *stats) |
| 860 | { |
| 861 | stats->total_time = 0; |
| 862 | stats->busy_time = 0; |
| 863 | } |
| 864 | |
| 865 | static void z180_irqctrl(struct kgsl_device *device, int state) |
| 866 | { |
| 867 | /* Control interrupts for Z180 and the Z180 MMU */ |
| 868 | |
| 869 | if (state) { |
| 870 | z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 3); |
| 871 | z180_regwrite(device, MH_INTERRUPT_MASK, KGSL_MMU_INT_MASK); |
| 872 | } else { |
| 873 | z180_regwrite(device, (ADDR_VGC_IRQENABLE >> 2), 0); |
| 874 | z180_regwrite(device, MH_INTERRUPT_MASK, 0); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | static const struct kgsl_functable z180_functable = { |
| 879 | /* Mandatory functions */ |
| 880 | .regread = z180_regread, |
| 881 | .regwrite = z180_regwrite, |
| 882 | .idle = z180_idle, |
| 883 | .isidle = z180_isidle, |
| 884 | .suspend_context = z180_suspend_context, |
| 885 | .start = z180_start, |
| 886 | .stop = z180_stop, |
| 887 | .getproperty = z180_getproperty, |
| 888 | .waittimestamp = z180_waittimestamp, |
| 889 | .readtimestamp = z180_readtimestamp, |
| 890 | .issueibcmds = z180_cmdstream_issueibcmds, |
| 891 | .setup_pt = z180_setup_pt, |
| 892 | .cleanup_pt = z180_cleanup_pt, |
| 893 | .power_stats = z180_power_stats, |
| 894 | .irqctrl = z180_irqctrl, |
| 895 | /* Optional functions */ |
| 896 | .setstate = z180_setstate, |
| 897 | .drawctxt_create = NULL, |
| 898 | .drawctxt_destroy = z180_drawctxt_destroy, |
| 899 | .ioctl = NULL, |
| 900 | }; |
| 901 | |
| 902 | static struct platform_device_id z180_id_table[] = { |
| 903 | { DEVICE_2D0_NAME, (kernel_ulong_t)&device_2d0.dev, }, |
| 904 | { DEVICE_2D1_NAME, (kernel_ulong_t)&device_2d1.dev, }, |
| 905 | { }, |
| 906 | }; |
| 907 | MODULE_DEVICE_TABLE(platform, z180_id_table); |
| 908 | |
| 909 | static struct platform_driver z180_platform_driver = { |
| 910 | .probe = z180_probe, |
| 911 | .remove = __devexit_p(z180_remove), |
| 912 | .suspend = kgsl_suspend_driver, |
| 913 | .resume = kgsl_resume_driver, |
| 914 | .id_table = z180_id_table, |
| 915 | .driver = { |
| 916 | .owner = THIS_MODULE, |
| 917 | .name = DEVICE_2D_NAME, |
| 918 | .pm = &kgsl_pm_ops, |
| 919 | } |
| 920 | }; |
| 921 | |
| 922 | static int __init kgsl_2d_init(void) |
| 923 | { |
| 924 | return platform_driver_register(&z180_platform_driver); |
| 925 | } |
| 926 | |
| 927 | static void __exit kgsl_2d_exit(void) |
| 928 | { |
| 929 | platform_driver_unregister(&z180_platform_driver); |
| 930 | } |
| 931 | |
| 932 | module_init(kgsl_2d_init); |
| 933 | module_exit(kgsl_2d_exit); |
| 934 | |
| 935 | MODULE_DESCRIPTION("2D Graphics driver"); |
| 936 | MODULE_VERSION("1.2"); |
| 937 | MODULE_LICENSE("GPL v2"); |
| 938 | MODULE_ALIAS("platform:kgsl_2d"); |