Vijay Krishnamoorthy | bef6693 | 2012-01-24 09:32:05 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2002,2007-2012, Code Aurora Forum. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 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 | */ |
Jordan Crouse | a78c917 | 2011-07-11 13:14:09 -0600 | [diff] [blame] | 13 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 14 | #include <linux/slab.h> |
| 15 | |
| 16 | #include "kgsl.h" |
| 17 | #include "kgsl_sharedmem.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 18 | #include "adreno.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 19 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame^] | 20 | #define KGSL_INIT_REFTIMESTAMP 0x7FFFFFFF |
| 21 | |
Jordan Crouse | 0e0486f | 2011-07-28 08:37:58 -0600 | [diff] [blame] | 22 | /* quad for copying GMEM to context shadow */ |
| 23 | #define QUAD_LEN 12 |
Jordan Crouse | b4d31bd | 2012-02-01 22:11:12 -0700 | [diff] [blame] | 24 | #define QUAD_RESTORE_LEN 14 |
Jordan Crouse | 0e0486f | 2011-07-28 08:37:58 -0600 | [diff] [blame] | 25 | |
| 26 | static unsigned int gmem_copy_quad[QUAD_LEN] = { |
| 27 | 0x00000000, 0x00000000, 0x3f800000, |
| 28 | 0x00000000, 0x00000000, 0x3f800000, |
| 29 | 0x00000000, 0x00000000, 0x3f800000, |
| 30 | 0x00000000, 0x00000000, 0x3f800000 |
| 31 | }; |
| 32 | |
Jordan Crouse | b4d31bd | 2012-02-01 22:11:12 -0700 | [diff] [blame] | 33 | static unsigned int gmem_restore_quad[QUAD_RESTORE_LEN] = { |
| 34 | 0x00000000, 0x3f800000, 0x3f800000, |
| 35 | 0x00000000, 0x00000000, 0x00000000, |
| 36 | 0x3f800000, 0x00000000, 0x00000000, |
| 37 | 0x3f800000, 0x00000000, 0x00000000, |
| 38 | 0x3f800000, 0x3f800000, |
| 39 | }; |
| 40 | |
Jordan Crouse | 0e0486f | 2011-07-28 08:37:58 -0600 | [diff] [blame] | 41 | #define TEXCOORD_LEN 8 |
| 42 | |
| 43 | static unsigned int gmem_copy_texcoord[TEXCOORD_LEN] = { |
| 44 | 0x00000000, 0x3f800000, |
| 45 | 0x3f800000, 0x3f800000, |
| 46 | 0x00000000, 0x00000000, |
| 47 | 0x3f800000, 0x00000000 |
| 48 | }; |
| 49 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 50 | /* |
Jordan Crouse | a78c917 | 2011-07-11 13:14:09 -0600 | [diff] [blame] | 51 | * Helper functions |
| 52 | * These are global helper functions used by the GPUs during context switch |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 53 | */ |
| 54 | |
Jordan Crouse | a78c917 | 2011-07-11 13:14:09 -0600 | [diff] [blame] | 55 | /** |
| 56 | * uint2float - convert a uint to IEEE754 single precision float |
| 57 | * @ uintval - value to convert |
| 58 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 59 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 60 | unsigned int uint2float(unsigned int uintval) |
| 61 | { |
| 62 | unsigned int exp, frac = 0; |
| 63 | |
| 64 | if (uintval == 0) |
| 65 | return 0; |
| 66 | |
| 67 | exp = ilog2(uintval); |
| 68 | |
| 69 | /* Calculate fraction */ |
| 70 | if (23 > exp) |
| 71 | frac = (uintval & (~(1 << exp))) << (23 - exp); |
| 72 | |
| 73 | /* Exp is biased by 127 and shifted 23 bits */ |
| 74 | exp = (exp + 127) << 23; |
| 75 | |
| 76 | return exp | frac; |
| 77 | } |
| 78 | |
Jordan Crouse | 0e0486f | 2011-07-28 08:37:58 -0600 | [diff] [blame] | 79 | static void set_gmem_copy_quad(struct gmem_shadow_t *shadow) |
| 80 | { |
| 81 | /* set vertex buffer values */ |
| 82 | gmem_copy_quad[1] = uint2float(shadow->height); |
| 83 | gmem_copy_quad[3] = uint2float(shadow->width); |
| 84 | gmem_copy_quad[4] = uint2float(shadow->height); |
| 85 | gmem_copy_quad[9] = uint2float(shadow->width); |
| 86 | |
Jordan Crouse | b4d31bd | 2012-02-01 22:11:12 -0700 | [diff] [blame] | 87 | gmem_restore_quad[5] = uint2float(shadow->height); |
| 88 | gmem_restore_quad[7] = uint2float(shadow->width); |
Jordan Crouse | 0e0486f | 2011-07-28 08:37:58 -0600 | [diff] [blame] | 89 | |
| 90 | memcpy(shadow->quad_vertices.hostptr, gmem_copy_quad, QUAD_LEN << 2); |
Tarun Karra | 7e8e1cf | 2012-02-06 18:23:19 -0800 | [diff] [blame] | 91 | memcpy(shadow->quad_vertices_restore.hostptr, gmem_restore_quad, |
Jordan Crouse | b4d31bd | 2012-02-01 22:11:12 -0700 | [diff] [blame] | 92 | QUAD_RESTORE_LEN << 2); |
Jordan Crouse | 0e0486f | 2011-07-28 08:37:58 -0600 | [diff] [blame] | 93 | |
| 94 | memcpy(shadow->quad_texcoords.hostptr, gmem_copy_texcoord, |
| 95 | TEXCOORD_LEN << 2); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * build_quad_vtxbuff - Create a quad for saving/restoring GMEM |
| 100 | * @ context - Pointer to the context being created |
| 101 | * @ shadow - Pointer to the GMEM shadow structure |
| 102 | * @ incmd - Pointer to pointer to the temporary command buffer |
| 103 | */ |
| 104 | |
| 105 | /* quad for saving/restoring gmem */ |
| 106 | void build_quad_vtxbuff(struct adreno_context *drawctxt, |
| 107 | struct gmem_shadow_t *shadow, unsigned int **incmd) |
| 108 | { |
| 109 | unsigned int *cmd = *incmd; |
| 110 | |
| 111 | /* quad vertex buffer location (in GPU space) */ |
| 112 | shadow->quad_vertices.hostptr = cmd; |
| 113 | shadow->quad_vertices.gpuaddr = virt2gpu(cmd, &drawctxt->gpustate); |
| 114 | |
| 115 | cmd += QUAD_LEN; |
| 116 | |
Jordan Crouse | b4d31bd | 2012-02-01 22:11:12 -0700 | [diff] [blame] | 117 | /* Used by A3XX, but define for both to make the code easier */ |
| 118 | shadow->quad_vertices_restore.hostptr = cmd; |
| 119 | shadow->quad_vertices_restore.gpuaddr = |
| 120 | virt2gpu(cmd, &drawctxt->gpustate); |
| 121 | |
| 122 | cmd += QUAD_RESTORE_LEN; |
| 123 | |
Jordan Crouse | 0e0486f | 2011-07-28 08:37:58 -0600 | [diff] [blame] | 124 | /* tex coord buffer location (in GPU space) */ |
| 125 | shadow->quad_texcoords.hostptr = cmd; |
| 126 | shadow->quad_texcoords.gpuaddr = virt2gpu(cmd, &drawctxt->gpustate); |
| 127 | |
| 128 | cmd += TEXCOORD_LEN; |
| 129 | |
| 130 | set_gmem_copy_quad(shadow); |
| 131 | *incmd = cmd; |
| 132 | } |
| 133 | |
Jordan Crouse | a78c917 | 2011-07-11 13:14:09 -0600 | [diff] [blame] | 134 | /** |
| 135 | * adreno_drawctxt_create - create a new adreno draw context |
| 136 | * @device - KGSL device to create the context on |
| 137 | * @pagetable - Pagetable for the context |
| 138 | * @context- Generic KGSL context structure |
| 139 | * @flags - flags for the context (passed from user space) |
| 140 | * |
| 141 | * Create a new draw context for the 3D core. Return 0 on success, |
| 142 | * or error code on failure. |
| 143 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 144 | int adreno_drawctxt_create(struct kgsl_device *device, |
| 145 | struct kgsl_pagetable *pagetable, |
| 146 | struct kgsl_context *context, uint32_t flags) |
| 147 | { |
| 148 | struct adreno_context *drawctxt; |
| 149 | struct adreno_device *adreno_dev = ADRENO_DEVICE(device); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 150 | int ret; |
| 151 | |
| 152 | drawctxt = kzalloc(sizeof(struct adreno_context), GFP_KERNEL); |
| 153 | |
| 154 | if (drawctxt == NULL) |
| 155 | return -ENOMEM; |
| 156 | |
| 157 | drawctxt->pagetable = pagetable; |
| 158 | drawctxt->bin_base_offset = 0; |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame^] | 159 | drawctxt->id = context->id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 160 | |
Vijay Krishnamoorthy | bef6693 | 2012-01-24 09:32:05 -0700 | [diff] [blame] | 161 | if (flags & KGSL_CONTEXT_PREAMBLE) |
| 162 | drawctxt->flags |= CTXT_FLAGS_PREAMBLE; |
Jordan Crouse | a78c917 | 2011-07-11 13:14:09 -0600 | [diff] [blame] | 163 | |
Vijay Krishnamoorthy | bef6693 | 2012-01-24 09:32:05 -0700 | [diff] [blame] | 164 | if (flags & KGSL_CONTEXT_NO_GMEM_ALLOC) |
| 165 | drawctxt->flags |= CTXT_FLAGS_NOGMEMALLOC; |
| 166 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame^] | 167 | if (flags & KGSL_CONTEXT_PER_CONTEXT_TS) |
| 168 | drawctxt->flags |= CTXT_FLAGS_PER_CONTEXT_TS; |
| 169 | |
Vijay Krishnamoorthy | bef6693 | 2012-01-24 09:32:05 -0700 | [diff] [blame] | 170 | ret = adreno_dev->gpudev->ctxt_create(adreno_dev, drawctxt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 171 | if (ret) |
| 172 | goto err; |
| 173 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame^] | 174 | kgsl_sharedmem_writel(&device->memstore, |
| 175 | KGSL_MEMSTORE_OFFSET(drawctxt->id, ref_wait_ts), |
| 176 | KGSL_INIT_REFTIMESTAMP); |
| 177 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 178 | context->devctxt = drawctxt; |
| 179 | return 0; |
| 180 | err: |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 181 | kfree(drawctxt); |
| 182 | return ret; |
| 183 | } |
| 184 | |
Jordan Crouse | a78c917 | 2011-07-11 13:14:09 -0600 | [diff] [blame] | 185 | /** |
| 186 | * adreno_drawctxt_destroy - destroy a draw context |
| 187 | * @device - KGSL device that owns the context |
| 188 | * @context- Generic KGSL context container for the context |
| 189 | * |
| 190 | * Destroy an existing context. Return 0 on success or error |
| 191 | * code on failure. |
| 192 | */ |
| 193 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 194 | /* destroy a drawing context */ |
| 195 | |
| 196 | void adreno_drawctxt_destroy(struct kgsl_device *device, |
| 197 | struct kgsl_context *context) |
| 198 | { |
| 199 | struct adreno_device *adreno_dev = ADRENO_DEVICE(device); |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame^] | 200 | struct adreno_context *drawctxt; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 201 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame^] | 202 | if (context == NULL) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 203 | return; |
| 204 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame^] | 205 | drawctxt = context->devctxt; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 206 | /* deactivate context */ |
| 207 | if (adreno_dev->drawctxt_active == drawctxt) { |
| 208 | /* no need to save GMEM or shader, the context is |
| 209 | * being destroyed. |
| 210 | */ |
| 211 | drawctxt->flags &= ~(CTXT_FLAGS_GMEM_SAVE | |
| 212 | CTXT_FLAGS_SHADER_SAVE | |
| 213 | CTXT_FLAGS_GMEM_SHADOW | |
| 214 | CTXT_FLAGS_STATE_SHADOW); |
| 215 | |
| 216 | adreno_drawctxt_switch(adreno_dev, NULL, 0); |
| 217 | } |
| 218 | |
| 219 | adreno_idle(device, KGSL_TIMEOUT_DEFAULT); |
| 220 | |
| 221 | kgsl_sharedmem_free(&drawctxt->gpustate); |
| 222 | kgsl_sharedmem_free(&drawctxt->context_gmem_shadow.gmemshadow); |
| 223 | |
| 224 | kfree(drawctxt); |
| 225 | context->devctxt = NULL; |
| 226 | } |
| 227 | |
Jordan Crouse | a78c917 | 2011-07-11 13:14:09 -0600 | [diff] [blame] | 228 | /** |
| 229 | * adreno_drawctxt_set_bin_base_offset - set bin base offset for the context |
| 230 | * @device - KGSL device that owns the context |
| 231 | * @context- Generic KGSL context container for the context |
| 232 | * @offset - Offset to set |
| 233 | * |
| 234 | * Set the bin base offset for A2XX devices. Not valid for A3XX devices. |
| 235 | */ |
| 236 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 237 | void adreno_drawctxt_set_bin_base_offset(struct kgsl_device *device, |
| 238 | struct kgsl_context *context, |
| 239 | unsigned int offset) |
| 240 | { |
| 241 | struct adreno_context *drawctxt = context->devctxt; |
| 242 | |
| 243 | if (drawctxt) |
| 244 | drawctxt->bin_base_offset = offset; |
| 245 | } |
| 246 | |
Jordan Crouse | a78c917 | 2011-07-11 13:14:09 -0600 | [diff] [blame] | 247 | /** |
| 248 | * adreno_drawctxt_switch - switch the current draw context |
| 249 | * @adreno_dev - The 3D device that owns the context |
| 250 | * @drawctxt - the 3D context to switch to |
| 251 | * @flags - Flags to accompany the switch (from user space) |
| 252 | * |
| 253 | * Switch the current draw context |
| 254 | */ |
| 255 | |
| 256 | void adreno_drawctxt_switch(struct adreno_device *adreno_dev, |
| 257 | struct adreno_context *drawctxt, |
| 258 | unsigned int flags) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 259 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 260 | struct kgsl_device *device = &adreno_dev->dev; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 261 | |
| 262 | if (drawctxt) { |
| 263 | if (flags & KGSL_CONTEXT_SAVE_GMEM) |
| 264 | /* Set the flag in context so that the save is done |
| 265 | * when this context is switched out. */ |
| 266 | drawctxt->flags |= CTXT_FLAGS_GMEM_SAVE; |
| 267 | else |
| 268 | /* Remove GMEM saving flag from the context */ |
| 269 | drawctxt->flags &= ~CTXT_FLAGS_GMEM_SAVE; |
| 270 | } |
Jordan Crouse | a78c917 | 2011-07-11 13:14:09 -0600 | [diff] [blame] | 271 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 272 | /* already current? */ |
Jordan Crouse | a78c917 | 2011-07-11 13:14:09 -0600 | [diff] [blame] | 273 | if (adreno_dev->drawctxt_active == drawctxt) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 274 | return; |
| 275 | |
| 276 | KGSL_CTXT_INFO(device, "from %p to %p flags %d\n", |
| 277 | adreno_dev->drawctxt_active, drawctxt, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 278 | |
Jordan Crouse | a78c917 | 2011-07-11 13:14:09 -0600 | [diff] [blame] | 279 | /* Save the old context */ |
| 280 | adreno_dev->gpudev->ctxt_save(adreno_dev, adreno_dev->drawctxt_active); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 281 | |
Jordan Crouse | a78c917 | 2011-07-11 13:14:09 -0600 | [diff] [blame] | 282 | /* Set the new context */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 283 | adreno_dev->drawctxt_active = drawctxt; |
Jordan Crouse | a78c917 | 2011-07-11 13:14:09 -0600 | [diff] [blame] | 284 | adreno_dev->gpudev->ctxt_restore(adreno_dev, drawctxt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 285 | } |