Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Jerome Glisse. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | * Software is furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice (including the next |
| 13 | * paragraph) shall be included in all copies or substantial portions of the |
| 14 | * Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | * DEALINGS IN THE SOFTWARE. |
| 23 | * |
| 24 | * Authors: |
| 25 | * Jerome Glisse <glisse@freedesktop.org> |
| 26 | */ |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 27 | #include <drm/drmP.h> |
| 28 | #include <drm/radeon_drm.h> |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 29 | #include "radeon_reg.h" |
| 30 | #include "radeon.h" |
| 31 | |
Lauri Kasanen | 1109ca0 | 2012-08-31 13:43:50 -0400 | [diff] [blame] | 32 | static int radeon_cs_parser_relocs(struct radeon_cs_parser *p) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 33 | { |
| 34 | struct drm_device *ddev = p->rdev->ddev; |
| 35 | struct radeon_cs_chunk *chunk; |
| 36 | unsigned i, j; |
| 37 | bool duplicate; |
| 38 | |
| 39 | if (p->chunk_relocs_idx == -1) { |
| 40 | return 0; |
| 41 | } |
| 42 | chunk = &p->chunks[p->chunk_relocs_idx]; |
Alex Deucher | cf4ccd0 | 2011-11-18 10:19:47 -0500 | [diff] [blame] | 43 | p->dma_reloc_idx = 0; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 44 | /* FIXME: we assume that each relocs use 4 dwords */ |
| 45 | p->nrelocs = chunk->length_dw / 4; |
| 46 | p->relocs_ptr = kcalloc(p->nrelocs, sizeof(void *), GFP_KERNEL); |
| 47 | if (p->relocs_ptr == NULL) { |
| 48 | return -ENOMEM; |
| 49 | } |
| 50 | p->relocs = kcalloc(p->nrelocs, sizeof(struct radeon_cs_reloc), GFP_KERNEL); |
| 51 | if (p->relocs == NULL) { |
| 52 | return -ENOMEM; |
| 53 | } |
| 54 | for (i = 0; i < p->nrelocs; i++) { |
| 55 | struct drm_radeon_cs_reloc *r; |
| 56 | |
| 57 | duplicate = false; |
| 58 | r = (struct drm_radeon_cs_reloc *)&chunk->kdata[i*4]; |
Christian König | 16557f1 | 2011-10-24 14:59:17 +0200 | [diff] [blame] | 59 | for (j = 0; j < i; j++) { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 60 | if (r->handle == p->relocs[j].handle) { |
| 61 | p->relocs_ptr[i] = &p->relocs[j]; |
| 62 | duplicate = true; |
| 63 | break; |
| 64 | } |
| 65 | } |
Christian König | 4474f3a | 2013-04-08 12:41:28 +0200 | [diff] [blame] | 66 | if (duplicate) { |
Christian König | 16557f1 | 2011-10-24 14:59:17 +0200 | [diff] [blame] | 67 | p->relocs[i].handle = 0; |
Christian König | 4474f3a | 2013-04-08 12:41:28 +0200 | [diff] [blame] | 68 | continue; |
| 69 | } |
| 70 | |
| 71 | p->relocs[i].gobj = drm_gem_object_lookup(ddev, p->filp, |
| 72 | r->handle); |
| 73 | if (p->relocs[i].gobj == NULL) { |
| 74 | DRM_ERROR("gem object lookup failed 0x%x\n", |
| 75 | r->handle); |
| 76 | return -ENOENT; |
| 77 | } |
| 78 | p->relocs_ptr[i] = &p->relocs[i]; |
| 79 | p->relocs[i].robj = gem_to_radeon_bo(p->relocs[i].gobj); |
| 80 | p->relocs[i].lobj.bo = p->relocs[i].robj; |
| 81 | p->relocs[i].lobj.written = !!r->write_domain; |
| 82 | |
Christian König | 075bf1f | 2013-09-15 13:31:28 +0200 | [diff] [blame] | 83 | /* the first reloc of an UVD job is the msg and that must be in |
| 84 | VRAM, also but everything into VRAM on AGP cards to avoid |
| 85 | image corruptions */ |
| 86 | if (p->ring == R600_RING_TYPE_UVD_INDEX && |
Alex Deucher | 6452230 | 2013-09-15 23:23:07 -0400 | [diff] [blame] | 87 | (i == 0 || drm_pci_device_is_agp(p->rdev->ddev))) { |
Christian König | f2ba57b | 2013-04-08 12:41:29 +0200 | [diff] [blame] | 88 | /* TODO: is this still needed for NI+ ? */ |
| 89 | p->relocs[i].lobj.domain = |
| 90 | RADEON_GEM_DOMAIN_VRAM; |
| 91 | |
| 92 | p->relocs[i].lobj.alt_domain = |
| 93 | RADEON_GEM_DOMAIN_VRAM; |
| 94 | |
| 95 | } else { |
| 96 | uint32_t domain = r->write_domain ? |
| 97 | r->write_domain : r->read_domains; |
| 98 | |
Marek Olšák | 9ca04cf | 2014-05-27 02:56:36 +0200 | [diff] [blame] | 99 | if (domain & RADEON_GEM_DOMAIN_CPU) { |
| 100 | DRM_ERROR("RADEON_GEM_DOMAIN_CPU is not valid " |
| 101 | "for command submission\n"); |
| 102 | return -EINVAL; |
| 103 | } |
| 104 | |
Christian König | f2ba57b | 2013-04-08 12:41:29 +0200 | [diff] [blame] | 105 | p->relocs[i].lobj.domain = domain; |
| 106 | if (domain == RADEON_GEM_DOMAIN_VRAM) |
| 107 | domain |= RADEON_GEM_DOMAIN_GTT; |
| 108 | p->relocs[i].lobj.alt_domain = domain; |
| 109 | } |
Christian König | 4474f3a | 2013-04-08 12:41:28 +0200 | [diff] [blame] | 110 | |
| 111 | p->relocs[i].lobj.tv.bo = &p->relocs[i].robj->tbo; |
| 112 | p->relocs[i].handle = r->handle; |
| 113 | |
| 114 | radeon_bo_list_add_object(&p->relocs[i].lobj, |
| 115 | &p->validated); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 116 | } |
Christian König | f2ba57b | 2013-04-08 12:41:29 +0200 | [diff] [blame] | 117 | return radeon_bo_list_validate(&p->validated, p->ring); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 120 | static int radeon_cs_get_ring(struct radeon_cs_parser *p, u32 ring, s32 priority) |
| 121 | { |
| 122 | p->priority = priority; |
| 123 | |
| 124 | switch (ring) { |
| 125 | default: |
| 126 | DRM_ERROR("unknown ring id: %d\n", ring); |
| 127 | return -EINVAL; |
| 128 | case RADEON_CS_RING_GFX: |
| 129 | p->ring = RADEON_RING_TYPE_GFX_INDEX; |
| 130 | break; |
| 131 | case RADEON_CS_RING_COMPUTE: |
Alex Deucher | 8d5ef7b | 2012-03-20 17:18:24 -0400 | [diff] [blame] | 132 | if (p->rdev->family >= CHIP_TAHITI) { |
| 133 | if (p->priority > 0) |
| 134 | p->ring = CAYMAN_RING_TYPE_CP1_INDEX; |
| 135 | else |
| 136 | p->ring = CAYMAN_RING_TYPE_CP2_INDEX; |
| 137 | } else |
| 138 | p->ring = RADEON_RING_TYPE_GFX_INDEX; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 139 | break; |
Alex Deucher | 278a334 | 2012-12-13 12:27:28 -0500 | [diff] [blame] | 140 | case RADEON_CS_RING_DMA: |
| 141 | if (p->rdev->family >= CHIP_CAYMAN) { |
| 142 | if (p->priority > 0) |
| 143 | p->ring = R600_RING_TYPE_DMA_INDEX; |
| 144 | else |
| 145 | p->ring = CAYMAN_RING_TYPE_DMA1_INDEX; |
| 146 | } else if (p->rdev->family >= CHIP_R600) { |
| 147 | p->ring = R600_RING_TYPE_DMA_INDEX; |
| 148 | } else { |
| 149 | return -EINVAL; |
| 150 | } |
| 151 | break; |
Christian König | f2ba57b | 2013-04-08 12:41:29 +0200 | [diff] [blame] | 152 | case RADEON_CS_RING_UVD: |
| 153 | p->ring = R600_RING_TYPE_UVD_INDEX; |
| 154 | break; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 155 | } |
| 156 | return 0; |
| 157 | } |
| 158 | |
Christian König | 220907d | 2012-05-10 16:46:43 +0200 | [diff] [blame] | 159 | static void radeon_cs_sync_rings(struct radeon_cs_parser *p) |
Christian König | 93504fc | 2012-01-05 22:11:06 -0500 | [diff] [blame] | 160 | { |
Christian König | 220907d | 2012-05-10 16:46:43 +0200 | [diff] [blame] | 161 | int i; |
Christian König | 93504fc | 2012-01-05 22:11:06 -0500 | [diff] [blame] | 162 | |
Christian König | cdac550 | 2012-02-23 15:18:42 +0100 | [diff] [blame] | 163 | for (i = 0; i < p->nrelocs; i++) { |
Christian König | f82cbdd | 2012-08-09 16:35:36 +0200 | [diff] [blame] | 164 | if (!p->relocs[i].robj) |
Christian König | cdac550 | 2012-02-23 15:18:42 +0100 | [diff] [blame] | 165 | continue; |
| 166 | |
Alex Deucher | 43f1214 | 2013-02-01 17:32:42 +0100 | [diff] [blame] | 167 | radeon_ib_sync_to(&p->ib, p->relocs[i].robj->tbo.sync_obj); |
Christian König | cdac550 | 2012-02-23 15:18:42 +0100 | [diff] [blame] | 168 | } |
Christian König | 93504fc | 2012-01-05 22:11:06 -0500 | [diff] [blame] | 169 | } |
| 170 | |
Alex Deucher | 9b00147 | 2012-05-30 10:09:30 -0400 | [diff] [blame] | 171 | /* XXX: note that this is called from the legacy UMS CS ioctl as well */ |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 172 | int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data) |
| 173 | { |
| 174 | struct drm_radeon_cs *cs = data; |
| 175 | uint64_t *chunk_array_ptr; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 176 | unsigned size, i; |
| 177 | u32 ring = RADEON_CS_RING_GFX; |
| 178 | s32 priority = 0; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 179 | |
Tommi Rantala | e1cd17d | 2015-03-02 21:36:07 +0200 | [diff] [blame] | 180 | INIT_LIST_HEAD(&p->validated); |
| 181 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 182 | if (!cs->num_chunks) { |
| 183 | return 0; |
| 184 | } |
Tommi Rantala | e1cd17d | 2015-03-02 21:36:07 +0200 | [diff] [blame] | 185 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 186 | /* get chunks */ |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 187 | p->idx = 0; |
Jerome Glisse | f2e3922 | 2012-05-09 15:35:02 +0200 | [diff] [blame] | 188 | p->ib.sa_bo = NULL; |
| 189 | p->ib.semaphore = NULL; |
| 190 | p->const_ib.sa_bo = NULL; |
| 191 | p->const_ib.semaphore = NULL; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 192 | p->chunk_ib_idx = -1; |
| 193 | p->chunk_relocs_idx = -1; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 194 | p->chunk_flags_idx = -1; |
Alex Deucher | dfcf5f3 | 2012-03-20 17:18:14 -0400 | [diff] [blame] | 195 | p->chunk_const_ib_idx = -1; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 196 | p->chunks_array = kcalloc(cs->num_chunks, sizeof(uint64_t), GFP_KERNEL); |
| 197 | if (p->chunks_array == NULL) { |
| 198 | return -ENOMEM; |
| 199 | } |
| 200 | chunk_array_ptr = (uint64_t *)(unsigned long)(cs->chunks); |
| 201 | if (DRM_COPY_FROM_USER(p->chunks_array, chunk_array_ptr, |
| 202 | sizeof(uint64_t)*cs->num_chunks)) { |
| 203 | return -EFAULT; |
| 204 | } |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 205 | p->cs_flags = 0; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 206 | p->nchunks = cs->num_chunks; |
| 207 | p->chunks = kcalloc(p->nchunks, sizeof(struct radeon_cs_chunk), GFP_KERNEL); |
| 208 | if (p->chunks == NULL) { |
| 209 | return -ENOMEM; |
| 210 | } |
| 211 | for (i = 0; i < p->nchunks; i++) { |
| 212 | struct drm_radeon_cs_chunk __user **chunk_ptr = NULL; |
| 213 | struct drm_radeon_cs_chunk user_chunk; |
| 214 | uint32_t __user *cdata; |
| 215 | |
| 216 | chunk_ptr = (void __user*)(unsigned long)p->chunks_array[i]; |
| 217 | if (DRM_COPY_FROM_USER(&user_chunk, chunk_ptr, |
| 218 | sizeof(struct drm_radeon_cs_chunk))) { |
| 219 | return -EFAULT; |
| 220 | } |
Dave Airlie | 5176fdc | 2009-06-30 11:47:14 +1000 | [diff] [blame] | 221 | p->chunks[i].length_dw = user_chunk.length_dw; |
| 222 | p->chunks[i].kdata = NULL; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 223 | p->chunks[i].chunk_id = user_chunk.chunk_id; |
Ilija Hadzic | 580f839 | 2013-01-02 18:27:37 -0500 | [diff] [blame] | 224 | p->chunks[i].user_ptr = (void __user *)(unsigned long)user_chunk.chunk_data; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 225 | if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) { |
| 226 | p->chunk_relocs_idx = i; |
| 227 | } |
| 228 | if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) { |
| 229 | p->chunk_ib_idx = i; |
Dave Airlie | 5176fdc | 2009-06-30 11:47:14 +1000 | [diff] [blame] | 230 | /* zero length IB isn't useful */ |
| 231 | if (p->chunks[i].length_dw == 0) |
| 232 | return -EINVAL; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 233 | } |
Alex Deucher | dfcf5f3 | 2012-03-20 17:18:14 -0400 | [diff] [blame] | 234 | if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_CONST_IB) { |
| 235 | p->chunk_const_ib_idx = i; |
| 236 | /* zero length CONST IB isn't useful */ |
| 237 | if (p->chunks[i].length_dw == 0) |
| 238 | return -EINVAL; |
| 239 | } |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 240 | if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) { |
| 241 | p->chunk_flags_idx = i; |
| 242 | /* zero length flags aren't useful */ |
| 243 | if (p->chunks[i].length_dw == 0) |
| 244 | return -EINVAL; |
Marek Olšák | e70f224 | 2011-10-25 01:38:45 +0200 | [diff] [blame] | 245 | } |
Dave Airlie | 5176fdc | 2009-06-30 11:47:14 +1000 | [diff] [blame] | 246 | |
Dave Airlie | 513bcb4 | 2009-09-23 16:56:27 +1000 | [diff] [blame] | 247 | cdata = (uint32_t *)(unsigned long)user_chunk.chunk_data; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 248 | if ((p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) || |
| 249 | (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS)) { |
Dave Airlie | 513bcb4 | 2009-09-23 16:56:27 +1000 | [diff] [blame] | 250 | size = p->chunks[i].length_dw * sizeof(uint32_t); |
| 251 | p->chunks[i].kdata = kmalloc(size, GFP_KERNEL); |
| 252 | if (p->chunks[i].kdata == NULL) { |
| 253 | return -ENOMEM; |
| 254 | } |
| 255 | if (DRM_COPY_FROM_USER(p->chunks[i].kdata, |
| 256 | p->chunks[i].user_ptr, size)) { |
| 257 | return -EFAULT; |
| 258 | } |
Marek Olšák | e70f224 | 2011-10-25 01:38:45 +0200 | [diff] [blame] | 259 | if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) { |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 260 | p->cs_flags = p->chunks[i].kdata[0]; |
| 261 | if (p->chunks[i].length_dw > 1) |
| 262 | ring = p->chunks[i].kdata[1]; |
| 263 | if (p->chunks[i].length_dw > 2) |
| 264 | priority = (s32)p->chunks[i].kdata[2]; |
Marek Olšák | e70f224 | 2011-10-25 01:38:45 +0200 | [diff] [blame] | 265 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 266 | } |
| 267 | } |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 268 | |
Alex Deucher | 9b00147 | 2012-05-30 10:09:30 -0400 | [diff] [blame] | 269 | /* these are KMS only */ |
| 270 | if (p->rdev) { |
| 271 | if ((p->cs_flags & RADEON_CS_USE_VM) && |
| 272 | !p->rdev->vm_manager.enabled) { |
| 273 | DRM_ERROR("VM not active on asic!\n"); |
| 274 | return -EINVAL; |
| 275 | } |
| 276 | |
Alex Deucher | 9b00147 | 2012-05-30 10:09:30 -0400 | [diff] [blame] | 277 | if (radeon_cs_get_ring(p, ring, priority)) |
| 278 | return -EINVAL; |
Christian König | 5744904 | 2013-04-08 12:41:27 +0200 | [diff] [blame] | 279 | |
| 280 | /* we only support VM on some SI+ rings */ |
| 281 | if ((p->rdev->asic->ring[p->ring].cs_parse == NULL) && |
| 282 | ((p->cs_flags & RADEON_CS_USE_VM) == 0)) { |
| 283 | DRM_ERROR("Ring %d requires VM!\n", p->ring); |
| 284 | return -EINVAL; |
| 285 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 286 | } |
Marek Olšák | e70f224 | 2011-10-25 01:38:45 +0200 | [diff] [blame] | 287 | |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 288 | /* deal with non-vm */ |
| 289 | if ((p->chunk_ib_idx != -1) && |
| 290 | ((p->cs_flags & RADEON_CS_USE_VM) == 0) && |
| 291 | (p->chunks[p->chunk_ib_idx].chunk_id == RADEON_CHUNK_ID_IB)) { |
| 292 | if (p->chunks[p->chunk_ib_idx].length_dw > (16 * 1024)) { |
| 293 | DRM_ERROR("cs IB too big: %d\n", |
| 294 | p->chunks[p->chunk_ib_idx].length_dw); |
| 295 | return -EINVAL; |
| 296 | } |
Ilija Hadzic | ff4bd08 | 2013-01-07 18:21:57 -0500 | [diff] [blame] | 297 | if (p->rdev && (p->rdev->flags & RADEON_IS_AGP)) { |
Dave Airlie | 6a7068b | 2012-04-03 16:23:41 +0100 | [diff] [blame] | 298 | p->chunks[p->chunk_ib_idx].kpage[0] = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 299 | p->chunks[p->chunk_ib_idx].kpage[1] = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 300 | if (p->chunks[p->chunk_ib_idx].kpage[0] == NULL || |
| 301 | p->chunks[p->chunk_ib_idx].kpage[1] == NULL) { |
Ilija Hadzic | 25d8999 | 2013-01-07 18:21:59 -0500 | [diff] [blame] | 302 | kfree(p->chunks[p->chunk_ib_idx].kpage[0]); |
| 303 | kfree(p->chunks[p->chunk_ib_idx].kpage[1]); |
Ilija Hadzic | 1da80cf | 2013-01-23 13:59:05 -0500 | [diff] [blame] | 304 | p->chunks[p->chunk_ib_idx].kpage[0] = NULL; |
| 305 | p->chunks[p->chunk_ib_idx].kpage[1] = NULL; |
Dave Airlie | 6a7068b | 2012-04-03 16:23:41 +0100 | [diff] [blame] | 306 | return -ENOMEM; |
| 307 | } |
| 308 | } |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 309 | p->chunks[p->chunk_ib_idx].kpage_idx[0] = -1; |
| 310 | p->chunks[p->chunk_ib_idx].kpage_idx[1] = -1; |
| 311 | p->chunks[p->chunk_ib_idx].last_copied_page = -1; |
| 312 | p->chunks[p->chunk_ib_idx].last_page_index = |
| 313 | ((p->chunks[p->chunk_ib_idx].length_dw * 4) - 1) / PAGE_SIZE; |
| 314 | } |
| 315 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * cs_parser_fini() - clean parser states |
| 321 | * @parser: parser structure holding parsing context. |
| 322 | * @error: error number |
| 323 | * |
| 324 | * If error is set than unvalidate buffer, otherwise just free memory |
| 325 | * used by parsing context. |
| 326 | **/ |
| 327 | static void radeon_cs_parser_fini(struct radeon_cs_parser *parser, int error) |
| 328 | { |
| 329 | unsigned i; |
| 330 | |
Jerome Glisse | e43b5ec | 2012-08-06 12:32:21 -0400 | [diff] [blame] | 331 | if (!error) { |
Thomas Hellstrom | 147666f | 2010-11-17 12:38:32 +0000 | [diff] [blame] | 332 | ttm_eu_fence_buffer_objects(&parser->validated, |
Jerome Glisse | f2e3922 | 2012-05-09 15:35:02 +0200 | [diff] [blame] | 333 | parser->ib.fence); |
Jerome Glisse | e43b5ec | 2012-08-06 12:32:21 -0400 | [diff] [blame] | 334 | } else { |
Thomas Hellstrom | 147666f | 2010-11-17 12:38:32 +0000 | [diff] [blame] | 335 | ttm_eu_backoff_reservation(&parser->validated); |
Jerome Glisse | e43b5ec | 2012-08-06 12:32:21 -0400 | [diff] [blame] | 336 | } |
Thomas Hellstrom | 147666f | 2010-11-17 12:38:32 +0000 | [diff] [blame] | 337 | |
Pauli Nieminen | fcbc451 | 2010-03-19 07:44:33 +0000 | [diff] [blame] | 338 | if (parser->relocs != NULL) { |
| 339 | for (i = 0; i < parser->nrelocs; i++) { |
| 340 | if (parser->relocs[i].gobj) |
| 341 | drm_gem_object_unreference_unlocked(parser->relocs[i].gobj); |
| 342 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 343 | } |
Michel Dänzer | 48e113e | 2009-09-15 17:09:32 +0200 | [diff] [blame] | 344 | kfree(parser->track); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 345 | kfree(parser->relocs); |
| 346 | kfree(parser->relocs_ptr); |
| 347 | for (i = 0; i < parser->nchunks; i++) { |
| 348 | kfree(parser->chunks[i].kdata); |
Dave Airlie | 6a7068b | 2012-04-03 16:23:41 +0100 | [diff] [blame] | 349 | if ((parser->rdev->flags & RADEON_IS_AGP)) { |
| 350 | kfree(parser->chunks[i].kpage[0]); |
| 351 | kfree(parser->chunks[i].kpage[1]); |
| 352 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 353 | } |
| 354 | kfree(parser->chunks); |
| 355 | kfree(parser->chunks_array); |
| 356 | radeon_ib_free(parser->rdev, &parser->ib); |
Jerome Glisse | f2e3922 | 2012-05-09 15:35:02 +0200 | [diff] [blame] | 357 | radeon_ib_free(parser->rdev, &parser->const_ib); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 358 | } |
| 359 | |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 360 | static int radeon_cs_ib_chunk(struct radeon_device *rdev, |
| 361 | struct radeon_cs_parser *parser) |
| 362 | { |
| 363 | struct radeon_cs_chunk *ib_chunk; |
| 364 | int r; |
| 365 | |
| 366 | if (parser->chunk_ib_idx == -1) |
| 367 | return 0; |
| 368 | |
| 369 | if (parser->cs_flags & RADEON_CS_USE_VM) |
| 370 | return 0; |
| 371 | |
| 372 | ib_chunk = &parser->chunks[parser->chunk_ib_idx]; |
| 373 | /* Copy the packet into the IB, the parser will read from the |
| 374 | * input memory (cached) and write to the IB (which can be |
| 375 | * uncached). |
| 376 | */ |
| 377 | r = radeon_ib_get(rdev, parser->ring, &parser->ib, |
Christian König | 4bf3dd9 | 2012-08-06 18:57:44 +0200 | [diff] [blame] | 378 | NULL, ib_chunk->length_dw * 4); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 379 | if (r) { |
| 380 | DRM_ERROR("Failed to get ib !\n"); |
| 381 | return r; |
| 382 | } |
Jerome Glisse | f2e3922 | 2012-05-09 15:35:02 +0200 | [diff] [blame] | 383 | parser->ib.length_dw = ib_chunk->length_dw; |
Christian König | eb0c19c | 2012-02-23 15:18:44 +0100 | [diff] [blame] | 384 | r = radeon_cs_parse(rdev, parser->ring, parser); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 385 | if (r || parser->parser_error) { |
| 386 | DRM_ERROR("Invalid command stream !\n"); |
| 387 | return r; |
| 388 | } |
| 389 | r = radeon_cs_finish_pages(parser); |
| 390 | if (r) { |
| 391 | DRM_ERROR("Invalid command stream !\n"); |
| 392 | return r; |
| 393 | } |
Christian König | 220907d | 2012-05-10 16:46:43 +0200 | [diff] [blame] | 394 | radeon_cs_sync_rings(parser); |
Christian König | 4ef7256 | 2012-07-13 13:06:00 +0200 | [diff] [blame] | 395 | r = radeon_ib_schedule(rdev, &parser->ib, NULL); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 396 | if (r) { |
| 397 | DRM_ERROR("Failed to schedule IB !\n"); |
| 398 | } |
Christian König | 93bf888 | 2012-07-03 14:05:41 +0200 | [diff] [blame] | 399 | return r; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static int radeon_bo_vm_update_pte(struct radeon_cs_parser *parser, |
| 403 | struct radeon_vm *vm) |
| 404 | { |
Jerome Glisse | 3e8970f | 2012-08-13 12:07:33 -0400 | [diff] [blame] | 405 | struct radeon_device *rdev = parser->rdev; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 406 | struct radeon_bo_list *lobj; |
| 407 | struct radeon_bo *bo; |
| 408 | int r; |
| 409 | |
Jerome Glisse | 3e8970f | 2012-08-13 12:07:33 -0400 | [diff] [blame] | 410 | r = radeon_vm_bo_update_pte(rdev, vm, rdev->ring_tmp_bo.bo, &rdev->ring_tmp_bo.bo->tbo.mem); |
| 411 | if (r) { |
| 412 | return r; |
| 413 | } |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 414 | list_for_each_entry(lobj, &parser->validated, tv.head) { |
| 415 | bo = lobj->bo; |
| 416 | r = radeon_vm_bo_update_pte(parser->rdev, vm, bo, &bo->tbo.mem); |
| 417 | if (r) { |
| 418 | return r; |
| 419 | } |
| 420 | } |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static int radeon_cs_ib_vm_chunk(struct radeon_device *rdev, |
| 425 | struct radeon_cs_parser *parser) |
| 426 | { |
| 427 | struct radeon_cs_chunk *ib_chunk; |
| 428 | struct radeon_fpriv *fpriv = parser->filp->driver_priv; |
| 429 | struct radeon_vm *vm = &fpriv->vm; |
| 430 | int r; |
| 431 | |
| 432 | if (parser->chunk_ib_idx == -1) |
| 433 | return 0; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 434 | if ((parser->cs_flags & RADEON_CS_USE_VM) == 0) |
| 435 | return 0; |
| 436 | |
Alex Deucher | dfcf5f3 | 2012-03-20 17:18:14 -0400 | [diff] [blame] | 437 | if ((rdev->family >= CHIP_TAHITI) && |
| 438 | (parser->chunk_const_ib_idx != -1)) { |
| 439 | ib_chunk = &parser->chunks[parser->chunk_const_ib_idx]; |
| 440 | if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) { |
| 441 | DRM_ERROR("cs IB CONST too big: %d\n", ib_chunk->length_dw); |
| 442 | return -EINVAL; |
| 443 | } |
| 444 | r = radeon_ib_get(rdev, parser->ring, &parser->const_ib, |
Christian König | 4bf3dd9 | 2012-08-06 18:57:44 +0200 | [diff] [blame] | 445 | vm, ib_chunk->length_dw * 4); |
Alex Deucher | dfcf5f3 | 2012-03-20 17:18:14 -0400 | [diff] [blame] | 446 | if (r) { |
| 447 | DRM_ERROR("Failed to get const ib !\n"); |
| 448 | return r; |
| 449 | } |
Jerome Glisse | f2e3922 | 2012-05-09 15:35:02 +0200 | [diff] [blame] | 450 | parser->const_ib.is_const_ib = true; |
| 451 | parser->const_ib.length_dw = ib_chunk->length_dw; |
Alex Deucher | dfcf5f3 | 2012-03-20 17:18:14 -0400 | [diff] [blame] | 452 | /* Copy the packet into the IB */ |
Jerome Glisse | f2e3922 | 2012-05-09 15:35:02 +0200 | [diff] [blame] | 453 | if (DRM_COPY_FROM_USER(parser->const_ib.ptr, ib_chunk->user_ptr, |
Alex Deucher | dfcf5f3 | 2012-03-20 17:18:14 -0400 | [diff] [blame] | 454 | ib_chunk->length_dw * 4)) { |
| 455 | return -EFAULT; |
| 456 | } |
Jerome Glisse | f2e3922 | 2012-05-09 15:35:02 +0200 | [diff] [blame] | 457 | r = radeon_ring_ib_parse(rdev, parser->ring, &parser->const_ib); |
Alex Deucher | dfcf5f3 | 2012-03-20 17:18:14 -0400 | [diff] [blame] | 458 | if (r) { |
| 459 | return r; |
| 460 | } |
| 461 | } |
| 462 | |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 463 | ib_chunk = &parser->chunks[parser->chunk_ib_idx]; |
| 464 | if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) { |
| 465 | DRM_ERROR("cs IB too big: %d\n", ib_chunk->length_dw); |
| 466 | return -EINVAL; |
| 467 | } |
| 468 | r = radeon_ib_get(rdev, parser->ring, &parser->ib, |
Christian König | 4bf3dd9 | 2012-08-06 18:57:44 +0200 | [diff] [blame] | 469 | vm, ib_chunk->length_dw * 4); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 470 | if (r) { |
| 471 | DRM_ERROR("Failed to get ib !\n"); |
| 472 | return r; |
| 473 | } |
Jerome Glisse | f2e3922 | 2012-05-09 15:35:02 +0200 | [diff] [blame] | 474 | parser->ib.length_dw = ib_chunk->length_dw; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 475 | /* Copy the packet into the IB */ |
Jerome Glisse | f2e3922 | 2012-05-09 15:35:02 +0200 | [diff] [blame] | 476 | if (DRM_COPY_FROM_USER(parser->ib.ptr, ib_chunk->user_ptr, |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 477 | ib_chunk->length_dw * 4)) { |
| 478 | return -EFAULT; |
| 479 | } |
Jerome Glisse | f2e3922 | 2012-05-09 15:35:02 +0200 | [diff] [blame] | 480 | r = radeon_ring_ib_parse(rdev, parser->ring, &parser->ib); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 481 | if (r) { |
| 482 | return r; |
| 483 | } |
| 484 | |
Christian König | 36ff39c | 2012-05-09 10:07:08 +0200 | [diff] [blame] | 485 | mutex_lock(&rdev->vm_manager.lock); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 486 | mutex_lock(&vm->mutex); |
Christian König | ddf03f5 | 2012-08-09 20:02:28 +0200 | [diff] [blame] | 487 | r = radeon_vm_alloc_pt(rdev, vm); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 488 | if (r) { |
| 489 | goto out; |
| 490 | } |
| 491 | r = radeon_bo_vm_update_pte(parser, vm); |
| 492 | if (r) { |
| 493 | goto out; |
| 494 | } |
Christian König | 220907d | 2012-05-10 16:46:43 +0200 | [diff] [blame] | 495 | radeon_cs_sync_rings(parser); |
Alex Deucher | 43f1214 | 2013-02-01 17:32:42 +0100 | [diff] [blame] | 496 | radeon_ib_sync_to(&parser->ib, vm->fence); |
| 497 | radeon_ib_sync_to(&parser->ib, radeon_vm_grab_id( |
| 498 | rdev, vm, parser->ring)); |
Christian König | 4ef7256 | 2012-07-13 13:06:00 +0200 | [diff] [blame] | 499 | |
Alex Deucher | dfcf5f3 | 2012-03-20 17:18:14 -0400 | [diff] [blame] | 500 | if ((rdev->family >= CHIP_TAHITI) && |
| 501 | (parser->chunk_const_ib_idx != -1)) { |
Christian König | 4ef7256 | 2012-07-13 13:06:00 +0200 | [diff] [blame] | 502 | r = radeon_ib_schedule(rdev, &parser->ib, &parser->const_ib); |
| 503 | } else { |
| 504 | r = radeon_ib_schedule(rdev, &parser->ib, NULL); |
Alex Deucher | dfcf5f3 | 2012-03-20 17:18:14 -0400 | [diff] [blame] | 505 | } |
| 506 | |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 507 | if (!r) { |
Christian König | ee60e29 | 2012-08-09 16:21:08 +0200 | [diff] [blame] | 508 | radeon_vm_fence(rdev, vm, parser->ib.fence); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 509 | } |
Christian König | ee60e29 | 2012-08-09 16:21:08 +0200 | [diff] [blame] | 510 | |
| 511 | out: |
Christian König | 13e55c3 | 2012-10-09 13:31:19 +0200 | [diff] [blame] | 512 | radeon_vm_add_to_lru(rdev, vm); |
Christian König | 36ff39c | 2012-05-09 10:07:08 +0200 | [diff] [blame] | 513 | mutex_unlock(&vm->mutex); |
| 514 | mutex_unlock(&rdev->vm_manager.lock); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 515 | return r; |
| 516 | } |
| 517 | |
Christian König | 6c6f478 | 2012-05-02 15:11:19 +0200 | [diff] [blame] | 518 | static int radeon_cs_handle_lockup(struct radeon_device *rdev, int r) |
| 519 | { |
| 520 | if (r == -EDEADLK) { |
| 521 | r = radeon_gpu_reset(rdev); |
| 522 | if (!r) |
| 523 | r = -EAGAIN; |
| 524 | } |
| 525 | return r; |
| 526 | } |
| 527 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 528 | int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) |
| 529 | { |
| 530 | struct radeon_device *rdev = dev->dev_private; |
| 531 | struct radeon_cs_parser parser; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 532 | int r; |
| 533 | |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 534 | down_read(&rdev->exclusive_lock); |
Jerome Glisse | 6b7746e | 2012-02-20 17:57:20 -0500 | [diff] [blame] | 535 | if (!rdev->accel_working) { |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 536 | up_read(&rdev->exclusive_lock); |
Jerome Glisse | 6b7746e | 2012-02-20 17:57:20 -0500 | [diff] [blame] | 537 | return -EBUSY; |
| 538 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 539 | /* initialize parser */ |
| 540 | memset(&parser, 0, sizeof(struct radeon_cs_parser)); |
| 541 | parser.filp = filp; |
| 542 | parser.rdev = rdev; |
Jerome Glisse | c8c15ff | 2010-01-18 13:01:36 +0100 | [diff] [blame] | 543 | parser.dev = rdev->dev; |
Dave Airlie | 428c6e3 | 2011-06-08 19:58:29 +1000 | [diff] [blame] | 544 | parser.family = rdev->family; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 545 | r = radeon_cs_parser_init(&parser, data); |
| 546 | if (r) { |
| 547 | DRM_ERROR("Failed to initialize parser !\n"); |
| 548 | radeon_cs_parser_fini(&parser, r); |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 549 | up_read(&rdev->exclusive_lock); |
Christian König | 6c6f478 | 2012-05-02 15:11:19 +0200 | [diff] [blame] | 550 | r = radeon_cs_handle_lockup(rdev, r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 551 | return r; |
| 552 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 553 | r = radeon_cs_parser_relocs(&parser); |
| 554 | if (r) { |
Dave Airlie | 97f23b3 | 2010-03-19 10:33:44 +1000 | [diff] [blame] | 555 | if (r != -ERESTARTSYS) |
| 556 | DRM_ERROR("Failed to parse relocation %d!\n", r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 557 | radeon_cs_parser_fini(&parser, r); |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 558 | up_read(&rdev->exclusive_lock); |
Christian König | 6c6f478 | 2012-05-02 15:11:19 +0200 | [diff] [blame] | 559 | r = radeon_cs_handle_lockup(rdev, r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 560 | return r; |
| 561 | } |
Christian König | 55b51c8 | 2013-04-18 15:25:59 +0200 | [diff] [blame] | 562 | |
| 563 | if (parser.ring == R600_RING_TYPE_UVD_INDEX) |
| 564 | radeon_uvd_note_usage(rdev); |
| 565 | |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 566 | r = radeon_cs_ib_chunk(rdev, &parser); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 567 | if (r) { |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 568 | goto out; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 569 | } |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 570 | r = radeon_cs_ib_vm_chunk(rdev, &parser); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 571 | if (r) { |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 572 | goto out; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 573 | } |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 574 | out: |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 575 | radeon_cs_parser_fini(&parser, r); |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 576 | up_read(&rdev->exclusive_lock); |
Christian König | 6c6f478 | 2012-05-02 15:11:19 +0200 | [diff] [blame] | 577 | r = radeon_cs_handle_lockup(rdev, r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 578 | return r; |
| 579 | } |
Dave Airlie | 513bcb4 | 2009-09-23 16:56:27 +1000 | [diff] [blame] | 580 | |
| 581 | int radeon_cs_finish_pages(struct radeon_cs_parser *p) |
| 582 | { |
| 583 | struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx]; |
| 584 | int i; |
| 585 | int size = PAGE_SIZE; |
| 586 | |
| 587 | for (i = ibc->last_copied_page + 1; i <= ibc->last_page_index; i++) { |
| 588 | if (i == ibc->last_page_index) { |
| 589 | size = (ibc->length_dw * 4) % PAGE_SIZE; |
| 590 | if (size == 0) |
| 591 | size = PAGE_SIZE; |
| 592 | } |
| 593 | |
Jerome Glisse | f2e3922 | 2012-05-09 15:35:02 +0200 | [diff] [blame] | 594 | if (DRM_COPY_FROM_USER(p->ib.ptr + (i * (PAGE_SIZE/4)), |
Dave Airlie | 513bcb4 | 2009-09-23 16:56:27 +1000 | [diff] [blame] | 595 | ibc->user_ptr + (i * PAGE_SIZE), |
| 596 | size)) |
| 597 | return -EFAULT; |
| 598 | } |
| 599 | return 0; |
| 600 | } |
| 601 | |
Dave Airlie | c4c7f31 | 2012-05-26 17:34:24 +0100 | [diff] [blame] | 602 | static int radeon_cs_update_pages(struct radeon_cs_parser *p, int pg_idx) |
Dave Airlie | 513bcb4 | 2009-09-23 16:56:27 +1000 | [diff] [blame] | 603 | { |
| 604 | int new_page; |
Dave Airlie | 513bcb4 | 2009-09-23 16:56:27 +1000 | [diff] [blame] | 605 | struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx]; |
| 606 | int i; |
| 607 | int size = PAGE_SIZE; |
Ilija Hadzic | ff4bd08 | 2013-01-07 18:21:57 -0500 | [diff] [blame] | 608 | bool copy1 = (p->rdev && (p->rdev->flags & RADEON_IS_AGP)) ? |
| 609 | false : true; |
Dave Airlie | 513bcb4 | 2009-09-23 16:56:27 +1000 | [diff] [blame] | 610 | |
Dave Airlie | c5e617e | 2009-09-26 09:03:39 +1000 | [diff] [blame] | 611 | for (i = ibc->last_copied_page + 1; i < pg_idx; i++) { |
Jerome Glisse | f2e3922 | 2012-05-09 15:35:02 +0200 | [diff] [blame] | 612 | if (DRM_COPY_FROM_USER(p->ib.ptr + (i * (PAGE_SIZE/4)), |
Dave Airlie | 513bcb4 | 2009-09-23 16:56:27 +1000 | [diff] [blame] | 613 | ibc->user_ptr + (i * PAGE_SIZE), |
| 614 | PAGE_SIZE)) { |
| 615 | p->parser_error = -EFAULT; |
| 616 | return 0; |
| 617 | } |
| 618 | } |
| 619 | |
Dave Airlie | 513bcb4 | 2009-09-23 16:56:27 +1000 | [diff] [blame] | 620 | if (pg_idx == ibc->last_page_index) { |
| 621 | size = (ibc->length_dw * 4) % PAGE_SIZE; |
Dave Airlie | 6a7068b | 2012-04-03 16:23:41 +0100 | [diff] [blame] | 622 | if (size == 0) |
| 623 | size = PAGE_SIZE; |
Dave Airlie | 513bcb4 | 2009-09-23 16:56:27 +1000 | [diff] [blame] | 624 | } |
| 625 | |
Dave Airlie | 6a7068b | 2012-04-03 16:23:41 +0100 | [diff] [blame] | 626 | new_page = ibc->kpage_idx[0] < ibc->kpage_idx[1] ? 0 : 1; |
| 627 | if (copy1) |
Jerome Glisse | f2e3922 | 2012-05-09 15:35:02 +0200 | [diff] [blame] | 628 | ibc->kpage[new_page] = p->ib.ptr + (pg_idx * (PAGE_SIZE / 4)); |
Dave Airlie | 6a7068b | 2012-04-03 16:23:41 +0100 | [diff] [blame] | 629 | |
Dave Airlie | 513bcb4 | 2009-09-23 16:56:27 +1000 | [diff] [blame] | 630 | if (DRM_COPY_FROM_USER(ibc->kpage[new_page], |
| 631 | ibc->user_ptr + (pg_idx * PAGE_SIZE), |
| 632 | size)) { |
| 633 | p->parser_error = -EFAULT; |
| 634 | return 0; |
| 635 | } |
| 636 | |
Dave Airlie | 6a7068b | 2012-04-03 16:23:41 +0100 | [diff] [blame] | 637 | /* copy to IB for non single case */ |
| 638 | if (!copy1) |
Jerome Glisse | f2e3922 | 2012-05-09 15:35:02 +0200 | [diff] [blame] | 639 | memcpy((void *)(p->ib.ptr+(pg_idx*(PAGE_SIZE/4))), ibc->kpage[new_page], size); |
Dave Airlie | 513bcb4 | 2009-09-23 16:56:27 +1000 | [diff] [blame] | 640 | |
| 641 | ibc->last_copied_page = pg_idx; |
| 642 | ibc->kpage_idx[new_page] = pg_idx; |
| 643 | |
| 644 | return new_page; |
| 645 | } |
Dave Airlie | c4c7f31 | 2012-05-26 17:34:24 +0100 | [diff] [blame] | 646 | |
| 647 | u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx) |
| 648 | { |
| 649 | struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx]; |
| 650 | u32 pg_idx, pg_offset; |
| 651 | u32 idx_value = 0; |
| 652 | int new_page; |
| 653 | |
| 654 | pg_idx = (idx * 4) / PAGE_SIZE; |
| 655 | pg_offset = (idx * 4) % PAGE_SIZE; |
| 656 | |
| 657 | if (ibc->kpage_idx[0] == pg_idx) |
| 658 | return ibc->kpage[0][pg_offset/4]; |
| 659 | if (ibc->kpage_idx[1] == pg_idx) |
| 660 | return ibc->kpage[1][pg_offset/4]; |
| 661 | |
| 662 | new_page = radeon_cs_update_pages(p, pg_idx); |
| 663 | if (new_page < 0) { |
| 664 | p->parser_error = new_page; |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | idx_value = ibc->kpage[new_page][pg_offset/4]; |
| 669 | return idx_value; |
| 670 | } |
Ilija Hadzic | 4db0131 | 2013-01-02 18:27:40 -0500 | [diff] [blame] | 671 | |
| 672 | /** |
| 673 | * radeon_cs_packet_parse() - parse cp packet and point ib index to next packet |
| 674 | * @parser: parser structure holding parsing context. |
| 675 | * @pkt: where to store packet information |
| 676 | * |
| 677 | * Assume that chunk_ib_index is properly set. Will return -EINVAL |
| 678 | * if packet is bigger than remaining ib size. or if packets is unknown. |
| 679 | **/ |
| 680 | int radeon_cs_packet_parse(struct radeon_cs_parser *p, |
| 681 | struct radeon_cs_packet *pkt, |
| 682 | unsigned idx) |
| 683 | { |
| 684 | struct radeon_cs_chunk *ib_chunk = &p->chunks[p->chunk_ib_idx]; |
| 685 | struct radeon_device *rdev = p->rdev; |
| 686 | uint32_t header; |
| 687 | |
| 688 | if (idx >= ib_chunk->length_dw) { |
| 689 | DRM_ERROR("Can not parse packet at %d after CS end %d !\n", |
| 690 | idx, ib_chunk->length_dw); |
| 691 | return -EINVAL; |
| 692 | } |
| 693 | header = radeon_get_ib_value(p, idx); |
| 694 | pkt->idx = idx; |
| 695 | pkt->type = RADEON_CP_PACKET_GET_TYPE(header); |
| 696 | pkt->count = RADEON_CP_PACKET_GET_COUNT(header); |
| 697 | pkt->one_reg_wr = 0; |
| 698 | switch (pkt->type) { |
| 699 | case RADEON_PACKET_TYPE0: |
| 700 | if (rdev->family < CHIP_R600) { |
| 701 | pkt->reg = R100_CP_PACKET0_GET_REG(header); |
| 702 | pkt->one_reg_wr = |
| 703 | RADEON_CP_PACKET0_GET_ONE_REG_WR(header); |
| 704 | } else |
| 705 | pkt->reg = R600_CP_PACKET0_GET_REG(header); |
| 706 | break; |
| 707 | case RADEON_PACKET_TYPE3: |
| 708 | pkt->opcode = RADEON_CP_PACKET3_GET_OPCODE(header); |
| 709 | break; |
| 710 | case RADEON_PACKET_TYPE2: |
| 711 | pkt->count = -1; |
| 712 | break; |
| 713 | default: |
| 714 | DRM_ERROR("Unknown packet type %d at %d !\n", pkt->type, idx); |
| 715 | return -EINVAL; |
| 716 | } |
| 717 | if ((pkt->count + 1 + pkt->idx) >= ib_chunk->length_dw) { |
| 718 | DRM_ERROR("Packet (%d:%d:%d) end after CS buffer (%d) !\n", |
| 719 | pkt->idx, pkt->type, pkt->count, ib_chunk->length_dw); |
| 720 | return -EINVAL; |
| 721 | } |
| 722 | return 0; |
| 723 | } |
Ilija Hadzic | 9ffb7a6 | 2013-01-02 18:27:42 -0500 | [diff] [blame] | 724 | |
| 725 | /** |
| 726 | * radeon_cs_packet_next_is_pkt3_nop() - test if the next packet is P3 NOP |
| 727 | * @p: structure holding the parser context. |
| 728 | * |
| 729 | * Check if the next packet is NOP relocation packet3. |
| 730 | **/ |
| 731 | bool radeon_cs_packet_next_is_pkt3_nop(struct radeon_cs_parser *p) |
| 732 | { |
| 733 | struct radeon_cs_packet p3reloc; |
| 734 | int r; |
| 735 | |
| 736 | r = radeon_cs_packet_parse(p, &p3reloc, p->idx); |
| 737 | if (r) |
| 738 | return false; |
| 739 | if (p3reloc.type != RADEON_PACKET_TYPE3) |
| 740 | return false; |
| 741 | if (p3reloc.opcode != RADEON_PACKET3_NOP) |
| 742 | return false; |
| 743 | return true; |
| 744 | } |
Ilija Hadzic | c3ad63a | 2013-01-02 18:27:45 -0500 | [diff] [blame] | 745 | |
| 746 | /** |
| 747 | * radeon_cs_dump_packet() - dump raw packet context |
| 748 | * @p: structure holding the parser context. |
| 749 | * @pkt: structure holding the packet. |
| 750 | * |
| 751 | * Used mostly for debugging and error reporting. |
| 752 | **/ |
| 753 | void radeon_cs_dump_packet(struct radeon_cs_parser *p, |
| 754 | struct radeon_cs_packet *pkt) |
| 755 | { |
| 756 | volatile uint32_t *ib; |
| 757 | unsigned i; |
| 758 | unsigned idx; |
| 759 | |
| 760 | ib = p->ib.ptr; |
| 761 | idx = pkt->idx; |
| 762 | for (i = 0; i <= (pkt->count + 1); i++, idx++) |
| 763 | DRM_INFO("ib[%d]=0x%08X\n", idx, ib[idx]); |
| 764 | } |
| 765 | |
Ilija Hadzic | e971699 | 2013-01-02 18:27:46 -0500 | [diff] [blame] | 766 | /** |
| 767 | * radeon_cs_packet_next_reloc() - parse next (should be reloc) packet |
| 768 | * @parser: parser structure holding parsing context. |
| 769 | * @data: pointer to relocation data |
| 770 | * @offset_start: starting offset |
| 771 | * @offset_mask: offset mask (to align start offset on) |
| 772 | * @reloc: reloc informations |
| 773 | * |
| 774 | * Check if next packet is relocation packet3, do bo validation and compute |
| 775 | * GPU offset using the provided start. |
| 776 | **/ |
| 777 | int radeon_cs_packet_next_reloc(struct radeon_cs_parser *p, |
| 778 | struct radeon_cs_reloc **cs_reloc, |
| 779 | int nomm) |
| 780 | { |
| 781 | struct radeon_cs_chunk *relocs_chunk; |
| 782 | struct radeon_cs_packet p3reloc; |
| 783 | unsigned idx; |
| 784 | int r; |
| 785 | |
| 786 | if (p->chunk_relocs_idx == -1) { |
| 787 | DRM_ERROR("No relocation chunk !\n"); |
| 788 | return -EINVAL; |
| 789 | } |
| 790 | *cs_reloc = NULL; |
| 791 | relocs_chunk = &p->chunks[p->chunk_relocs_idx]; |
| 792 | r = radeon_cs_packet_parse(p, &p3reloc, p->idx); |
| 793 | if (r) |
| 794 | return r; |
| 795 | p->idx += p3reloc.count + 2; |
| 796 | if (p3reloc.type != RADEON_PACKET_TYPE3 || |
| 797 | p3reloc.opcode != RADEON_PACKET3_NOP) { |
| 798 | DRM_ERROR("No packet3 for relocation for packet at %d.\n", |
| 799 | p3reloc.idx); |
| 800 | radeon_cs_dump_packet(p, &p3reloc); |
| 801 | return -EINVAL; |
| 802 | } |
| 803 | idx = radeon_get_ib_value(p, p3reloc.idx + 1); |
| 804 | if (idx >= relocs_chunk->length_dw) { |
| 805 | DRM_ERROR("Relocs at %d after relocations chunk end %d !\n", |
| 806 | idx, relocs_chunk->length_dw); |
| 807 | radeon_cs_dump_packet(p, &p3reloc); |
| 808 | return -EINVAL; |
| 809 | } |
| 810 | /* FIXME: we assume reloc size is 4 dwords */ |
| 811 | if (nomm) { |
| 812 | *cs_reloc = p->relocs; |
| 813 | (*cs_reloc)->lobj.gpu_offset = |
| 814 | (u64)relocs_chunk->kdata[idx + 3] << 32; |
| 815 | (*cs_reloc)->lobj.gpu_offset |= relocs_chunk->kdata[idx + 0]; |
| 816 | } else |
| 817 | *cs_reloc = p->relocs_ptr[(idx / 4)]; |
| 818 | return 0; |
| 819 | } |