1 // SPDX-License-Identifier: GPL-2.0-only OR MIT 2 /* Copyright (c) 2023 Imagination Technologies Ltd. */ 3 4 #include "pvr_mmu.h" 5 6 #include "pvr_ccb.h" 7 #include "pvr_device.h" 8 #include "pvr_fw.h" 9 #include "pvr_gem.h" 10 #include "pvr_power.h" 11 #include "pvr_rogue_fwif.h" 12 #include "pvr_rogue_mmu_defs.h" 13 14 #include <drm/drm_drv.h> 15 #include <drm/drm_print.h> 16 #include <linux/atomic.h> 17 #include <linux/bitops.h> 18 #include <linux/dma-mapping.h> 19 #include <linux/kmemleak.h> 20 #include <linux/minmax.h> 21 #include <linux/property.h> 22 #include <linux/sizes.h> 23 24 #define PVR_SHIFT_FROM_SIZE(size_) (__builtin_ctzll(size_)) 25 #define PVR_MASK_FROM_SIZE(size_) (~((size_) - U64_C(1))) 26 27 /* 28 * The value of the device page size (%PVR_DEVICE_PAGE_SIZE) is currently 29 * pegged to the host page size (%PAGE_SIZE). This chunk of macro goodness both 30 * ensures that the selected host page size corresponds to a valid device page 31 * size and sets up values needed by the MMU code below. 32 */ 33 #if (PVR_DEVICE_PAGE_SIZE == SZ_4K) 34 # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_4KB 35 # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_4KB_RANGE_SHIFT 36 # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_4KB_RANGE_CLRMSK 37 #elif (PVR_DEVICE_PAGE_SIZE == SZ_16K) 38 # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_16KB 39 # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_16KB_RANGE_SHIFT 40 # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_16KB_RANGE_CLRMSK 41 #elif (PVR_DEVICE_PAGE_SIZE == SZ_64K) 42 # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_64KB 43 # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_64KB_RANGE_SHIFT 44 # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_64KB_RANGE_CLRMSK 45 #elif (PVR_DEVICE_PAGE_SIZE == SZ_256K) 46 # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_256KB 47 # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_256KB_RANGE_SHIFT 48 # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_256KB_RANGE_CLRMSK 49 #elif (PVR_DEVICE_PAGE_SIZE == SZ_1M) 50 # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_1MB 51 # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_1MB_RANGE_SHIFT 52 # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_1MB_RANGE_CLRMSK 53 #elif (PVR_DEVICE_PAGE_SIZE == SZ_2M) 54 # define ROGUE_MMUCTRL_PAGE_SIZE_X ROGUE_MMUCTRL_PAGE_SIZE_2MB 55 # define ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT ROGUE_MMUCTRL_PAGE_2MB_RANGE_SHIFT 56 # define ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK ROGUE_MMUCTRL_PAGE_2MB_RANGE_CLRMSK 57 #else 58 # error Unsupported device page size PVR_DEVICE_PAGE_SIZE 59 #endif 60 61 #define ROGUE_MMUCTRL_ENTRIES_PT_VALUE_X \ 62 (ROGUE_MMUCTRL_ENTRIES_PT_VALUE >> \ 63 (PVR_DEVICE_PAGE_SHIFT - PVR_SHIFT_FROM_SIZE(SZ_4K))) 64 65 enum pvr_mmu_sync_level { 66 PVR_MMU_SYNC_LEVEL_NONE = -1, 67 PVR_MMU_SYNC_LEVEL_0 = 0, 68 PVR_MMU_SYNC_LEVEL_1 = 1, 69 PVR_MMU_SYNC_LEVEL_2 = 2, 70 }; 71 72 #define PVR_MMU_SYNC_LEVEL_0_FLAGS (ROGUE_FWIF_MMUCACHEDATA_FLAGS_PT | \ 73 ROGUE_FWIF_MMUCACHEDATA_FLAGS_INTERRUPT | \ 74 ROGUE_FWIF_MMUCACHEDATA_FLAGS_TLB) 75 #define PVR_MMU_SYNC_LEVEL_1_FLAGS (PVR_MMU_SYNC_LEVEL_0_FLAGS | ROGUE_FWIF_MMUCACHEDATA_FLAGS_PD) 76 #define PVR_MMU_SYNC_LEVEL_2_FLAGS (PVR_MMU_SYNC_LEVEL_1_FLAGS | ROGUE_FWIF_MMUCACHEDATA_FLAGS_PC) 77 78 /** 79 * pvr_mmu_set_flush_flags() - Set MMU cache flush flags for next call to 80 * pvr_mmu_flush_exec(). 81 * @pvr_dev: Target PowerVR device. 82 * @flags: MMU flush flags. Must be one of %PVR_MMU_SYNC_LEVEL_*_FLAGS. 83 * 84 * This function must be called following any possible change to the MMU page 85 * tables. 86 */ 87 static void pvr_mmu_set_flush_flags(struct pvr_device *pvr_dev, u32 flags) 88 { 89 atomic_fetch_or(flags, &pvr_dev->mmu_flush_cache_flags); 90 } 91 92 /** 93 * pvr_mmu_flush_request_all() - Request flush of all MMU caches when 94 * subsequently calling pvr_mmu_flush_exec(). 95 * @pvr_dev: Target PowerVR device. 96 * 97 * This function must be called following any possible change to the MMU page 98 * tables. 99 */ 100 void pvr_mmu_flush_request_all(struct pvr_device *pvr_dev) 101 { 102 pvr_mmu_set_flush_flags(pvr_dev, PVR_MMU_SYNC_LEVEL_2_FLAGS); 103 } 104 105 /** 106 * pvr_mmu_flush_exec() - Execute a flush of all MMU caches previously 107 * requested. 108 * @pvr_dev: Target PowerVR device. 109 * @wait: Do not return until the flush is completed. 110 * 111 * This function must be called prior to submitting any new GPU job. The flush 112 * will complete before the jobs are scheduled, so this can be called once after 113 * a series of maps. However, a single unmap should always be immediately 114 * followed by a flush and it should be explicitly waited by setting @wait. 115 * 116 * As a failure to flush the MMU caches could risk memory corruption, if the 117 * flush fails (implying the firmware is not responding) then the GPU device is 118 * marked as lost. 119 * 120 * Returns: 121 * * 0 on success when @wait is true, or 122 * * -%EIO if the device is unavailable, or 123 * * Any error encountered while submitting the flush command via the KCCB. 124 */ 125 int pvr_mmu_flush_exec(struct pvr_device *pvr_dev, bool wait) 126 { 127 struct rogue_fwif_kccb_cmd cmd_mmu_cache = {}; 128 struct rogue_fwif_mmucachedata *cmd_mmu_cache_data = 129 &cmd_mmu_cache.cmd_data.mmu_cache_data; 130 int err = 0; 131 u32 slot; 132 int idx; 133 134 if (!drm_dev_enter(from_pvr_device(pvr_dev), &idx)) 135 return -EIO; 136 137 /* Can't flush MMU if the firmware hasn't been initialised yet. */ 138 if (!READ_ONCE(pvr_dev->fw_dev.initialised)) 139 goto err_drm_dev_exit; 140 141 cmd_mmu_cache_data->cache_flags = 142 atomic_xchg(&pvr_dev->mmu_flush_cache_flags, 0); 143 144 if (!cmd_mmu_cache_data->cache_flags) 145 goto err_drm_dev_exit; 146 147 cmd_mmu_cache.cmd_type = ROGUE_FWIF_KCCB_CMD_MMUCACHE; 148 149 pvr_fw_object_get_fw_addr(pvr_dev->fw_dev.mem.mmucache_sync_obj, 150 &cmd_mmu_cache_data->mmu_cache_sync_fw_addr); 151 cmd_mmu_cache_data->mmu_cache_sync_update_value = 0; 152 153 err = pvr_kccb_send_cmd(pvr_dev, &cmd_mmu_cache, &slot); 154 if (err) 155 goto err_reset_and_retry; 156 157 err = pvr_kccb_wait_for_completion(pvr_dev, slot, HZ, NULL); 158 if (err) 159 goto err_reset_and_retry; 160 161 drm_dev_exit(idx); 162 163 return 0; 164 165 err_reset_and_retry: 166 /* 167 * Flush command failure is most likely the result of a firmware lockup. Hard 168 * reset the GPU and retry. 169 */ 170 err = pvr_power_reset(pvr_dev, true); 171 if (err) 172 goto err_drm_dev_exit; /* Device is lost. */ 173 174 /* Retry sending flush request. */ 175 err = pvr_kccb_send_cmd(pvr_dev, &cmd_mmu_cache, &slot); 176 if (err) { 177 pvr_device_lost(pvr_dev); 178 goto err_drm_dev_exit; 179 } 180 181 if (wait) { 182 err = pvr_kccb_wait_for_completion(pvr_dev, slot, HZ, NULL); 183 if (err) 184 pvr_device_lost(pvr_dev); 185 } 186 187 err_drm_dev_exit: 188 drm_dev_exit(idx); 189 190 return err; 191 } 192 193 /** 194 * DOC: PowerVR Virtual Memory Handling 195 */ 196 /** 197 * DOC: PowerVR Virtual Memory Handling (constants) 198 * 199 * .. c:macro:: PVR_IDX_INVALID 200 * 201 * Default value for a u16-based index. 202 * 203 * This value cannot be zero, since zero is a valid index value. 204 */ 205 #define PVR_IDX_INVALID ((u16)(-1)) 206 207 /** 208 * DOC: MMU backing pages 209 */ 210 /** 211 * DOC: MMU backing pages (constants) 212 * 213 * .. c:macro:: PVR_MMU_BACKING_PAGE_SIZE 214 * 215 * Page size of a PowerVR device's integrated MMU. The CPU page size must be 216 * at least as large as this value for the current implementation; this is 217 * checked at compile-time. 218 */ 219 #define PVR_MMU_BACKING_PAGE_SIZE SZ_4K 220 static_assert(PAGE_SIZE >= PVR_MMU_BACKING_PAGE_SIZE); 221 222 /** 223 * struct pvr_mmu_backing_page - Represents a single page used to back a page 224 * table of any level. 225 * @dma_addr: DMA address of this page. 226 * @host_ptr: CPU address of this page. 227 * @pvr_dev: The PowerVR device to which this page is associated. **For 228 * internal use only.** 229 */ 230 struct pvr_mmu_backing_page { 231 dma_addr_t dma_addr; 232 void *host_ptr; 233 /* private: internal use only */ 234 struct page *raw_page; 235 struct pvr_device *pvr_dev; 236 }; 237 238 /** 239 * pvr_mmu_backing_page_init() - Initialize a MMU backing page. 240 * @page: Target backing page. 241 * @pvr_dev: Target PowerVR device. 242 * 243 * This function performs three distinct operations: 244 * 245 * 1. Allocate a single page, 246 * 2. Map the page to the CPU, and 247 * 3. Map the page to DMA-space. 248 * 249 * It is expected that @page be zeroed (e.g. from kzalloc()) before calling 250 * this function. 251 * 252 * Return: 253 * * 0 on success, or 254 * * -%ENOMEM if allocation of the backing page or mapping of the backing 255 * page to DMA fails. 256 */ 257 static int 258 pvr_mmu_backing_page_init(struct pvr_mmu_backing_page *page, 259 struct pvr_device *pvr_dev) 260 { 261 struct device *dev = from_pvr_device(pvr_dev)->dev; 262 263 struct page *raw_page; 264 pgprot_t prot; 265 int err; 266 267 dma_addr_t dma_addr; 268 void *host_ptr; 269 270 raw_page = alloc_page(__GFP_ZERO | GFP_KERNEL); 271 if (!raw_page) 272 return -ENOMEM; 273 274 prot = PAGE_KERNEL; 275 if (device_get_dma_attr(dev) != DEV_DMA_COHERENT) 276 prot = pgprot_writecombine(prot); 277 278 host_ptr = vmap(&raw_page, 1, VM_MAP, prot); 279 if (!host_ptr) { 280 err = -ENOMEM; 281 goto err_free_page; 282 } 283 284 dma_addr = dma_map_page(dev, raw_page, 0, PVR_MMU_BACKING_PAGE_SIZE, 285 DMA_TO_DEVICE); 286 if (dma_mapping_error(dev, dma_addr)) { 287 err = -ENOMEM; 288 goto err_unmap_page; 289 } 290 291 page->dma_addr = dma_addr; 292 page->host_ptr = host_ptr; 293 page->pvr_dev = pvr_dev; 294 page->raw_page = raw_page; 295 kmemleak_alloc(page->host_ptr, PAGE_SIZE, 1, GFP_KERNEL); 296 297 return 0; 298 299 err_unmap_page: 300 vunmap(host_ptr); 301 302 err_free_page: 303 __free_page(raw_page); 304 305 return err; 306 } 307 308 /** 309 * pvr_mmu_backing_page_fini() - Teardown a MMU backing page. 310 * @page: Target backing page. 311 * 312 * This function performs the mirror operations to pvr_mmu_backing_page_init(), 313 * in reverse order: 314 * 315 * 1. Unmap the page from DMA-space, 316 * 2. Unmap the page from the CPU, and 317 * 3. Free the page. 318 * 319 * It also zeros @page. 320 * 321 * It is a no-op to call this function a second (or further) time on any @page. 322 */ 323 static void 324 pvr_mmu_backing_page_fini(struct pvr_mmu_backing_page *page) 325 { 326 struct device *dev; 327 328 /* Do nothing if no allocation is present. */ 329 if (!page->pvr_dev) 330 return; 331 332 dev = from_pvr_device(page->pvr_dev)->dev; 333 334 dma_unmap_page(dev, page->dma_addr, PVR_MMU_BACKING_PAGE_SIZE, 335 DMA_TO_DEVICE); 336 337 kmemleak_free(page->host_ptr); 338 vunmap(page->host_ptr); 339 340 __free_page(page->raw_page); 341 342 memset(page, 0, sizeof(*page)); 343 } 344 345 /** 346 * pvr_mmu_backing_page_sync() - Flush a MMU backing page from the CPU to the 347 * device. 348 * @page: Target backing page. 349 * @flags: MMU flush flags. Must be one of %PVR_MMU_SYNC_LEVEL_*_FLAGS. 350 * 351 * .. caution:: 352 * 353 * **This is potentially an expensive function call.** Only call 354 * pvr_mmu_backing_page_sync() once you're sure you have no more changes to 355 * make to the backing page in the immediate future. 356 */ 357 static void 358 pvr_mmu_backing_page_sync(struct pvr_mmu_backing_page *page, u32 flags) 359 { 360 struct pvr_device *pvr_dev = page->pvr_dev; 361 struct device *dev; 362 363 /* 364 * Do nothing if no allocation is present. This may be the case if 365 * we are unmapping pages. 366 */ 367 if (!pvr_dev) 368 return; 369 370 dev = from_pvr_device(pvr_dev)->dev; 371 372 dma_sync_single_for_device(dev, page->dma_addr, 373 PVR_MMU_BACKING_PAGE_SIZE, DMA_TO_DEVICE); 374 375 pvr_mmu_set_flush_flags(pvr_dev, flags); 376 } 377 378 /** 379 * DOC: Raw page tables 380 */ 381 382 #define PVR_PAGE_TABLE_TYPEOF_ENTRY(level_) \ 383 typeof_member(struct pvr_page_table_l##level_##_entry_raw, val) 384 385 #define PVR_PAGE_TABLE_FIELD_GET(level_, name_, field_, entry_) \ 386 (((entry_).val & \ 387 ~ROGUE_MMUCTRL_##name_##_DATA_##field_##_CLRMSK) >> \ 388 ROGUE_MMUCTRL_##name_##_DATA_##field_##_SHIFT) 389 390 #define PVR_PAGE_TABLE_FIELD_PREP(level_, name_, field_, val_) \ 391 ((((PVR_PAGE_TABLE_TYPEOF_ENTRY(level_))(val_)) \ 392 << ROGUE_MMUCTRL_##name_##_DATA_##field_##_SHIFT) & \ 393 ~ROGUE_MMUCTRL_##name_##_DATA_##field_##_CLRMSK) 394 395 /** 396 * struct pvr_page_table_l2_entry_raw - A single entry in a level 2 page table. 397 * @val: The raw value of this entry. 398 * 399 * This type is a structure for type-checking purposes. At compile-time, its 400 * size is checked against %ROGUE_MMUCTRL_ENTRY_SIZE_PC_VALUE. 401 * 402 * The value stored in this structure can be decoded using the following bitmap: 403 * 404 * .. flat-table:: 405 * :widths: 1 5 406 * :stub-columns: 1 407 * 408 * * - 31..4 409 * - **Level 1 Page Table Base Address:** Bits 39..12 of the L1 410 * page table base address, which is 4KiB aligned. 411 * 412 * * - 3..2 413 * - *(reserved)* 414 * 415 * * - 1 416 * - **Pending:** When valid bit is not set, indicates that a valid 417 * entry is pending and the MMU should wait for the driver to map 418 * the entry. This is used to support page demand mapping of 419 * memory. 420 * 421 * * - 0 422 * - **Valid:** Indicates that the entry contains a valid L1 page 423 * table. If the valid bit is not set, then an attempted use of 424 * the page would result in a page fault. 425 */ 426 struct pvr_page_table_l2_entry_raw { 427 u32 val; 428 } __packed; 429 static_assert(sizeof(struct pvr_page_table_l2_entry_raw) * 8 == 430 ROGUE_MMUCTRL_ENTRY_SIZE_PC_VALUE); 431 432 static bool 433 pvr_page_table_l2_entry_raw_is_valid(struct pvr_page_table_l2_entry_raw entry) 434 { 435 return PVR_PAGE_TABLE_FIELD_GET(2, PC, VALID, entry); 436 } 437 438 /** 439 * pvr_page_table_l2_entry_raw_set() - Write a valid entry into a raw level 2 440 * page table. 441 * @entry: Target raw level 2 page table entry. 442 * @child_table_dma_addr: DMA address of the level 1 page table to be 443 * associated with @entry. 444 * 445 * When calling this function, @child_table_dma_addr must be a valid DMA 446 * address and a multiple of %ROGUE_MMUCTRL_PC_DATA_PD_BASE_ALIGNSIZE. 447 */ 448 static void 449 pvr_page_table_l2_entry_raw_set(struct pvr_page_table_l2_entry_raw *entry, 450 dma_addr_t child_table_dma_addr) 451 { 452 child_table_dma_addr >>= ROGUE_MMUCTRL_PC_DATA_PD_BASE_ALIGNSHIFT; 453 454 WRITE_ONCE(entry->val, 455 PVR_PAGE_TABLE_FIELD_PREP(2, PC, VALID, true) | 456 PVR_PAGE_TABLE_FIELD_PREP(2, PC, ENTRY_PENDING, false) | 457 PVR_PAGE_TABLE_FIELD_PREP(2, PC, PD_BASE, child_table_dma_addr)); 458 } 459 460 static void 461 pvr_page_table_l2_entry_raw_clear(struct pvr_page_table_l2_entry_raw *entry) 462 { 463 WRITE_ONCE(entry->val, 0); 464 } 465 466 /** 467 * struct pvr_page_table_l1_entry_raw - A single entry in a level 1 page table. 468 * @val: The raw value of this entry. 469 * 470 * This type is a structure for type-checking purposes. At compile-time, its 471 * size is checked against %ROGUE_MMUCTRL_ENTRY_SIZE_PD_VALUE. 472 * 473 * The value stored in this structure can be decoded using the following bitmap: 474 * 475 * .. flat-table:: 476 * :widths: 1 5 477 * :stub-columns: 1 478 * 479 * * - 63..41 480 * - *(reserved)* 481 * 482 * * - 40 483 * - **Pending:** When valid bit is not set, indicates that a valid entry 484 * is pending and the MMU should wait for the driver to map the entry. 485 * This is used to support page demand mapping of memory. 486 * 487 * * - 39..5 488 * - **Level 0 Page Table Base Address:** The way this value is 489 * interpreted depends on the page size. Bits not specified in the 490 * table below (e.g. bits 11..5 for page size 4KiB) should be 491 * considered reserved. 492 * 493 * This table shows the bits used in an L1 page table entry to 494 * represent the Physical Table Base Address for a given Page Size. 495 * Since each L1 page table entry covers 2MiB of address space, the 496 * maximum page size is 2MiB. 497 * 498 * .. flat-table:: 499 * :widths: 1 1 1 1 500 * :header-rows: 1 501 * :stub-columns: 1 502 * 503 * * - Page size 504 * - L0 page table base address bits 505 * - Number of L0 page table entries 506 * - Size of L0 page table 507 * 508 * * - 4KiB 509 * - 39..12 510 * - 512 511 * - 4KiB 512 * 513 * * - 16KiB 514 * - 39..10 515 * - 128 516 * - 1KiB 517 * 518 * * - 64KiB 519 * - 39..8 520 * - 32 521 * - 256B 522 * 523 * * - 256KiB 524 * - 39..6 525 * - 8 526 * - 64B 527 * 528 * * - 1MiB 529 * - 39..5 (4 = '0') 530 * - 2 531 * - 16B 532 * 533 * * - 2MiB 534 * - 39..5 (4..3 = '00') 535 * - 1 536 * - 8B 537 * 538 * * - 4 539 * - *(reserved)* 540 * 541 * * - 3..1 542 * - **Page Size:** Sets the page size, from 4KiB to 2MiB. 543 * 544 * * - 0 545 * - **Valid:** Indicates that the entry contains a valid L0 page table. 546 * If the valid bit is not set, then an attempted use of the page would 547 * result in a page fault. 548 */ 549 struct pvr_page_table_l1_entry_raw { 550 u64 val; 551 } __packed; 552 static_assert(sizeof(struct pvr_page_table_l1_entry_raw) * 8 == 553 ROGUE_MMUCTRL_ENTRY_SIZE_PD_VALUE); 554 555 static bool 556 pvr_page_table_l1_entry_raw_is_valid(struct pvr_page_table_l1_entry_raw entry) 557 { 558 return PVR_PAGE_TABLE_FIELD_GET(1, PD, VALID, entry); 559 } 560 561 /** 562 * pvr_page_table_l1_entry_raw_set() - Write a valid entry into a raw level 1 563 * page table. 564 * @entry: Target raw level 1 page table entry. 565 * @child_table_dma_addr: DMA address of the level 0 page table to be 566 * associated with @entry. 567 * 568 * When calling this function, @child_table_dma_addr must be a valid DMA 569 * address and a multiple of 4 KiB. 570 */ 571 static void 572 pvr_page_table_l1_entry_raw_set(struct pvr_page_table_l1_entry_raw *entry, 573 dma_addr_t child_table_dma_addr) 574 { 575 WRITE_ONCE(entry->val, 576 PVR_PAGE_TABLE_FIELD_PREP(1, PD, VALID, true) | 577 PVR_PAGE_TABLE_FIELD_PREP(1, PD, ENTRY_PENDING, false) | 578 PVR_PAGE_TABLE_FIELD_PREP(1, PD, PAGE_SIZE, ROGUE_MMUCTRL_PAGE_SIZE_X) | 579 /* 580 * The use of a 4K-specific macro here is correct. It is 581 * a future optimization to allocate sub-host-page-sized 582 * blocks for individual tables, so the condition that any 583 * page table address is aligned to the size of the 584 * largest (a 4KB) table currently holds. 585 */ 586 (child_table_dma_addr & ~ROGUE_MMUCTRL_PT_BASE_4KB_RANGE_CLRMSK)); 587 } 588 589 static void 590 pvr_page_table_l1_entry_raw_clear(struct pvr_page_table_l1_entry_raw *entry) 591 { 592 WRITE_ONCE(entry->val, 0); 593 } 594 595 /** 596 * struct pvr_page_table_l0_entry_raw - A single entry in a level 0 page table. 597 * @val: The raw value of this entry. 598 * 599 * This type is a structure for type-checking purposes. At compile-time, its 600 * size is checked against %ROGUE_MMUCTRL_ENTRY_SIZE_PT_VALUE. 601 * 602 * The value stored in this structure can be decoded using the following bitmap: 603 * 604 * .. flat-table:: 605 * :widths: 1 5 606 * :stub-columns: 1 607 * 608 * * - 63 609 * - *(reserved)* 610 * 611 * * - 62 612 * - **PM/FW Protect:** Indicates a protected region which only the 613 * Parameter Manager (PM) or firmware processor can write to. 614 * 615 * * - 61..40 616 * - **VP Page (High):** Virtual-physical page used for Parameter Manager 617 * (PM) memory. This field is only used if the additional level of PB 618 * virtualization is enabled. The VP Page field is needed by the PM in 619 * order to correctly reconstitute the free lists after render 620 * completion. This (High) field holds bits 39..18 of the value; the 621 * Low field holds bits 17..12. Bits 11..0 are always zero because the 622 * value is always aligned to the 4KiB page size. 623 * 624 * * - 39..12 625 * - **Physical Page Address:** The way this value is interpreted depends 626 * on the page size. Bits not specified in the table below (e.g. bits 627 * 20..12 for page size 2MiB) should be considered reserved. 628 * 629 * This table shows the bits used in an L0 page table entry to represent 630 * the Physical Page Address for a given page size (as defined in the 631 * associated L1 page table entry). 632 * 633 * .. flat-table:: 634 * :widths: 1 1 635 * :header-rows: 1 636 * :stub-columns: 1 637 * 638 * * - Page size 639 * - Physical address bits 640 * 641 * * - 4KiB 642 * - 39..12 643 * 644 * * - 16KiB 645 * - 39..14 646 * 647 * * - 64KiB 648 * - 39..16 649 * 650 * * - 256KiB 651 * - 39..18 652 * 653 * * - 1MiB 654 * - 39..20 655 * 656 * * - 2MiB 657 * - 39..21 658 * 659 * * - 11..6 660 * - **VP Page (Low):** Continuation of VP Page (High). 661 * 662 * * - 5 663 * - **Pending:** When valid bit is not set, indicates that a valid entry 664 * is pending and the MMU should wait for the driver to map the entry. 665 * This is used to support page demand mapping of memory. 666 * 667 * * - 4 668 * - **PM Src:** Set on Parameter Manager (PM) allocated page table 669 * entries when indicated by the PM. Note that this bit will only be set 670 * by the PM, not by the device driver. 671 * 672 * * - 3 673 * - **SLC Bypass Control:** Specifies requests to this page should bypass 674 * the System Level Cache (SLC), if enabled in SLC configuration. 675 * 676 * * - 2 677 * - **Cache Coherency:** Indicates that the page is coherent (i.e. it 678 * does not require a cache flush between operations on the CPU and the 679 * device). 680 * 681 * * - 1 682 * - **Read Only:** If set, this bit indicates that the page is read only. 683 * An attempted write to this page would result in a write-protection 684 * fault. 685 * 686 * * - 0 687 * - **Valid:** Indicates that the entry contains a valid page. If the 688 * valid bit is not set, then an attempted use of the page would result 689 * in a page fault. 690 */ 691 struct pvr_page_table_l0_entry_raw { 692 u64 val; 693 } __packed; 694 static_assert(sizeof(struct pvr_page_table_l0_entry_raw) * 8 == 695 ROGUE_MMUCTRL_ENTRY_SIZE_PT_VALUE); 696 697 /** 698 * struct pvr_page_flags_raw - The configurable flags from a single entry in a 699 * level 0 page table. 700 * @val: The raw value of these flags. Since these are a strict subset of 701 * &struct pvr_page_table_l0_entry_raw; use that type for our member here. 702 * 703 * The flags stored in this type are: PM/FW Protect; SLC Bypass Control; Cache 704 * Coherency, and Read Only (bits 62, 3, 2 and 1 respectively). 705 * 706 * This type should never be instantiated directly; instead use 707 * pvr_page_flags_raw_create() to ensure only valid bits of @val are set. 708 */ 709 struct pvr_page_flags_raw { 710 struct pvr_page_table_l0_entry_raw val; 711 } __packed; 712 static_assert(sizeof(struct pvr_page_flags_raw) == 713 sizeof(struct pvr_page_table_l0_entry_raw)); 714 715 static bool 716 pvr_page_table_l0_entry_raw_is_valid(struct pvr_page_table_l0_entry_raw entry) 717 { 718 return PVR_PAGE_TABLE_FIELD_GET(0, PT, VALID, entry); 719 } 720 721 /** 722 * pvr_page_table_l0_entry_raw_set() - Write a valid entry into a raw level 0 723 * page table. 724 * @entry: Target raw level 0 page table entry. 725 * @dma_addr: DMA address of the physical page to be associated with @entry. 726 * @flags: Options to be set on @entry. 727 * 728 * When calling this function, @child_table_dma_addr must be a valid DMA 729 * address and a multiple of %PVR_DEVICE_PAGE_SIZE. 730 * 731 * The @flags parameter is directly assigned into @entry. It is the callers 732 * responsibility to ensure that only bits specified in 733 * &struct pvr_page_flags_raw are set in @flags. 734 */ 735 static void 736 pvr_page_table_l0_entry_raw_set(struct pvr_page_table_l0_entry_raw *entry, 737 dma_addr_t dma_addr, 738 struct pvr_page_flags_raw flags) 739 { 740 WRITE_ONCE(entry->val, PVR_PAGE_TABLE_FIELD_PREP(0, PT, VALID, true) | 741 PVR_PAGE_TABLE_FIELD_PREP(0, PT, ENTRY_PENDING, false) | 742 (dma_addr & ~ROGUE_MMUCTRL_PAGE_X_RANGE_CLRMSK) | 743 flags.val.val); 744 } 745 746 static void 747 pvr_page_table_l0_entry_raw_clear(struct pvr_page_table_l0_entry_raw *entry) 748 { 749 WRITE_ONCE(entry->val, 0); 750 } 751 752 /** 753 * pvr_page_flags_raw_create() - Initialize the flag bits of a raw level 0 page 754 * table entry. 755 * @read_only: This page is read-only (see: Read Only). 756 * @cache_coherent: This page does not require cache flushes (see: Cache 757 * Coherency). 758 * @slc_bypass: This page bypasses the device cache (see: SLC Bypass Control). 759 * @pm_fw_protect: This page is only for use by the firmware or Parameter 760 * Manager (see PM/FW Protect). 761 * 762 * For more details on the use of these four options, see their respective 763 * entries in the table under &struct pvr_page_table_l0_entry_raw. 764 * 765 * Return: 766 * A new &struct pvr_page_flags_raw instance which can be passed directly to 767 * pvr_page_table_l0_entry_raw_set() or pvr_page_table_l0_insert(). 768 */ 769 static struct pvr_page_flags_raw 770 pvr_page_flags_raw_create(bool read_only, bool cache_coherent, bool slc_bypass, 771 bool pm_fw_protect) 772 { 773 struct pvr_page_flags_raw flags; 774 775 flags.val.val = 776 PVR_PAGE_TABLE_FIELD_PREP(0, PT, READ_ONLY, read_only) | 777 PVR_PAGE_TABLE_FIELD_PREP(0, PT, CC, cache_coherent) | 778 PVR_PAGE_TABLE_FIELD_PREP(0, PT, SLC_BYPASS_CTRL, slc_bypass) | 779 PVR_PAGE_TABLE_FIELD_PREP(0, PT, PM_META_PROTECT, pm_fw_protect); 780 781 return flags; 782 } 783 784 /** 785 * struct pvr_page_table_l2_raw - The raw data of a level 2 page table. 786 * 787 * This type is a structure for type-checking purposes. At compile-time, its 788 * size is checked against %PVR_MMU_BACKING_PAGE_SIZE. 789 */ 790 struct pvr_page_table_l2_raw { 791 /** @entries: The raw values of this table. */ 792 struct pvr_page_table_l2_entry_raw 793 entries[ROGUE_MMUCTRL_ENTRIES_PC_VALUE]; 794 } __packed; 795 static_assert(sizeof(struct pvr_page_table_l2_raw) == PVR_MMU_BACKING_PAGE_SIZE); 796 797 /** 798 * struct pvr_page_table_l1_raw - The raw data of a level 1 page table. 799 * 800 * This type is a structure for type-checking purposes. At compile-time, its 801 * size is checked against %PVR_MMU_BACKING_PAGE_SIZE. 802 */ 803 struct pvr_page_table_l1_raw { 804 /** @entries: The raw values of this table. */ 805 struct pvr_page_table_l1_entry_raw 806 entries[ROGUE_MMUCTRL_ENTRIES_PD_VALUE]; 807 } __packed; 808 static_assert(sizeof(struct pvr_page_table_l1_raw) == PVR_MMU_BACKING_PAGE_SIZE); 809 810 /** 811 * struct pvr_page_table_l0_raw - The raw data of a level 0 page table. 812 * 813 * This type is a structure for type-checking purposes. At compile-time, its 814 * size is checked against %PVR_MMU_BACKING_PAGE_SIZE. 815 * 816 * .. caution:: 817 * 818 * The size of level 0 page tables is variable depending on the page size 819 * specified in the associated level 1 page table entry. Since the device 820 * page size in use is pegged to the host page size, it cannot vary at 821 * runtime. This structure is therefore only defined to contain the required 822 * number of entries for the current device page size. **You should never 823 * read or write beyond the last supported entry.** 824 */ 825 struct pvr_page_table_l0_raw { 826 /** @entries: The raw values of this table. */ 827 struct pvr_page_table_l0_entry_raw 828 entries[ROGUE_MMUCTRL_ENTRIES_PT_VALUE_X]; 829 } __packed; 830 static_assert(sizeof(struct pvr_page_table_l0_raw) <= PVR_MMU_BACKING_PAGE_SIZE); 831 832 /** 833 * DOC: Mirror page tables 834 */ 835 836 /* 837 * We pre-declare these types because they cross-depend on pointers to each 838 * other. 839 */ 840 struct pvr_page_table_l1; 841 struct pvr_page_table_l0; 842 843 /** 844 * struct pvr_page_table_l2 - A wrapped level 2 page table. 845 * 846 * To access the raw part of this table, use pvr_page_table_l2_get_raw(). 847 * Alternatively to access a raw entry directly, use 848 * pvr_page_table_l2_get_entry_raw(). 849 * 850 * A level 2 page table forms the root of the page table tree structure, so 851 * this type has no &parent or &parent_idx members. 852 */ 853 struct pvr_page_table_l2 { 854 /** 855 * @entries: The children of this node in the page table tree 856 * structure. These are also mirror tables. The indexing of this array 857 * is identical to that of the raw equivalent 858 * (&pvr_page_table_l1_raw.entries). 859 */ 860 struct pvr_page_table_l1 *entries[ROGUE_MMUCTRL_ENTRIES_PC_VALUE]; 861 862 /** 863 * @backing_page: A handle to the memory which holds the raw 864 * equivalent of this table. **For internal use only.** 865 */ 866 struct pvr_mmu_backing_page backing_page; 867 868 /** 869 * @entry_count: The current number of valid entries (that we know of) 870 * in this table. This value is essentially a refcount - the table is 871 * destroyed when this value is decremented to zero by 872 * pvr_page_table_l2_remove(). 873 */ 874 u16 entry_count; 875 }; 876 877 /** 878 * pvr_page_table_l2_init() - Initialize a level 2 page table. 879 * @table: Target level 2 page table. 880 * @pvr_dev: Target PowerVR device 881 * 882 * It is expected that @table be zeroed (e.g. from kzalloc()) before calling 883 * this function. 884 * 885 * Return: 886 * * 0 on success, or 887 * * Any error encountered while intializing &table->backing_page using 888 * pvr_mmu_backing_page_init(). 889 */ 890 static int 891 pvr_page_table_l2_init(struct pvr_page_table_l2 *table, 892 struct pvr_device *pvr_dev) 893 { 894 return pvr_mmu_backing_page_init(&table->backing_page, pvr_dev); 895 } 896 897 /** 898 * pvr_page_table_l2_fini() - Teardown a level 2 page table. 899 * @table: Target level 2 page table. 900 * 901 * It is an error to attempt to use @table after calling this function. 902 */ 903 static void 904 pvr_page_table_l2_fini(struct pvr_page_table_l2 *table) 905 { 906 pvr_mmu_backing_page_fini(&table->backing_page); 907 } 908 909 /** 910 * pvr_page_table_l2_sync() - Flush a level 2 page table from the CPU to the 911 * device. 912 * @table: Target level 2 page table. 913 * 914 * This is just a thin wrapper around pvr_mmu_backing_page_sync(), so the 915 * warning there applies here too: **Only call pvr_page_table_l2_sync() once 916 * you're sure you have no more changes to make to** @table **in the immediate 917 * future.** 918 * 919 * If child level 1 page tables of @table also need to be flushed, this should 920 * be done first using pvr_page_table_l1_sync() *before* calling this function. 921 */ 922 static void 923 pvr_page_table_l2_sync(struct pvr_page_table_l2 *table) 924 { 925 pvr_mmu_backing_page_sync(&table->backing_page, PVR_MMU_SYNC_LEVEL_2_FLAGS); 926 } 927 928 /** 929 * pvr_page_table_l2_get_raw() - Access the raw equivalent of a mirror level 2 930 * page table. 931 * @table: Target level 2 page table. 932 * 933 * Essentially returns the CPU address of the raw equivalent of @table, cast to 934 * a &struct pvr_page_table_l2_raw pointer. 935 * 936 * You probably want to call pvr_page_table_l2_get_entry_raw() instead. 937 * 938 * Return: 939 * The raw equivalent of @table. 940 */ 941 static struct pvr_page_table_l2_raw * 942 pvr_page_table_l2_get_raw(struct pvr_page_table_l2 *table) 943 { 944 return table->backing_page.host_ptr; 945 } 946 947 /** 948 * pvr_page_table_l2_get_entry_raw() - Access an entry from the raw equivalent 949 * of a mirror level 2 page table. 950 * @table: Target level 2 page table. 951 * @idx: Index of the entry to access. 952 * 953 * Technically this function returns a pointer to a slot in a raw level 2 page 954 * table, since the returned "entry" is not guaranteed to be valid. The caller 955 * must verify the validity of the entry at the returned address (perhaps using 956 * pvr_page_table_l2_entry_raw_is_valid()) before reading or overwriting it. 957 * 958 * The value of @idx is not checked here; it is the callers responsibility to 959 * ensure @idx refers to a valid index within @table before dereferencing the 960 * returned pointer. 961 * 962 * Return: 963 * A pointer to the requested raw level 2 page table entry. 964 */ 965 static struct pvr_page_table_l2_entry_raw * 966 pvr_page_table_l2_get_entry_raw(struct pvr_page_table_l2 *table, u16 idx) 967 { 968 return &pvr_page_table_l2_get_raw(table)->entries[idx]; 969 } 970 971 /** 972 * pvr_page_table_l2_entry_is_valid() - Check if a level 2 page table entry is 973 * marked as valid. 974 * @table: Target level 2 page table. 975 * @idx: Index of the entry to check. 976 * 977 * The value of @idx is not checked here; it is the callers responsibility to 978 * ensure @idx refers to a valid index within @table before calling this 979 * function. 980 */ 981 static bool 982 pvr_page_table_l2_entry_is_valid(struct pvr_page_table_l2 *table, u16 idx) 983 { 984 struct pvr_page_table_l2_entry_raw entry_raw = 985 *pvr_page_table_l2_get_entry_raw(table, idx); 986 987 return pvr_page_table_l2_entry_raw_is_valid(entry_raw); 988 } 989 990 /** 991 * struct pvr_page_table_l1 - A wrapped level 1 page table. 992 * 993 * To access the raw part of this table, use pvr_page_table_l1_get_raw(). 994 * Alternatively to access a raw entry directly, use 995 * pvr_page_table_l1_get_entry_raw(). 996 */ 997 struct pvr_page_table_l1 { 998 /** 999 * @entries: The children of this node in the page table tree 1000 * structure. These are also mirror tables. The indexing of this array 1001 * is identical to that of the raw equivalent 1002 * (&pvr_page_table_l0_raw.entries). 1003 */ 1004 struct pvr_page_table_l0 *entries[ROGUE_MMUCTRL_ENTRIES_PD_VALUE]; 1005 1006 /** 1007 * @backing_page: A handle to the memory which holds the raw 1008 * equivalent of this table. **For internal use only.** 1009 */ 1010 struct pvr_mmu_backing_page backing_page; 1011 1012 union { 1013 /** 1014 * @parent: The parent of this node in the page table tree structure. 1015 * 1016 * This is also a mirror table. 1017 * 1018 * Only valid when the L1 page table is active. When the L1 page table 1019 * has been removed and queued for destruction, the next_free field 1020 * should be used instead. 1021 */ 1022 struct pvr_page_table_l2 *parent; 1023 1024 /** 1025 * @next_free: Pointer to the next L1 page table to take/free. 1026 * 1027 * Used to form a linked list of L1 page tables. This is used 1028 * when preallocating tables and when the page table has been 1029 * removed and queued for destruction. 1030 */ 1031 struct pvr_page_table_l1 *next_free; 1032 }; 1033 1034 /** 1035 * @parent_idx: The index of the entry in the parent table (see 1036 * @parent) which corresponds to this table. 1037 */ 1038 u16 parent_idx; 1039 1040 /** 1041 * @entry_count: The current number of valid entries (that we know of) 1042 * in this table. This value is essentially a refcount - the table is 1043 * destroyed when this value is decremented to zero by 1044 * pvr_page_table_l1_remove(). 1045 */ 1046 u16 entry_count; 1047 }; 1048 1049 /** 1050 * pvr_page_table_l1_init() - Initialize a level 1 page table. 1051 * @table: Target level 1 page table. 1052 * @pvr_dev: Target PowerVR device 1053 * 1054 * When this function returns successfully, @table is still not considered 1055 * valid. It must be inserted into the page table tree structure with 1056 * pvr_page_table_l2_insert() before it is ready for use. 1057 * 1058 * It is expected that @table be zeroed (e.g. from kzalloc()) before calling 1059 * this function. 1060 * 1061 * Return: 1062 * * 0 on success, or 1063 * * Any error encountered while intializing &table->backing_page using 1064 * pvr_mmu_backing_page_init(). 1065 */ 1066 static int 1067 pvr_page_table_l1_init(struct pvr_page_table_l1 *table, 1068 struct pvr_device *pvr_dev) 1069 { 1070 table->parent_idx = PVR_IDX_INVALID; 1071 1072 return pvr_mmu_backing_page_init(&table->backing_page, pvr_dev); 1073 } 1074 1075 /** 1076 * pvr_page_table_l1_free() - Teardown a level 1 page table. 1077 * @table: Target level 1 page table. 1078 * 1079 * It is an error to attempt to use @table after calling this function, even 1080 * indirectly. This includes calling pvr_page_table_l2_remove(), which must 1081 * be called *before* pvr_page_table_l1_free(). 1082 */ 1083 static void 1084 pvr_page_table_l1_free(struct pvr_page_table_l1 *table) 1085 { 1086 pvr_mmu_backing_page_fini(&table->backing_page); 1087 kfree(table); 1088 } 1089 1090 /** 1091 * pvr_page_table_l1_sync() - Flush a level 1 page table from the CPU to the 1092 * device. 1093 * @table: Target level 1 page table. 1094 * 1095 * This is just a thin wrapper around pvr_mmu_backing_page_sync(), so the 1096 * warning there applies here too: **Only call pvr_page_table_l1_sync() once 1097 * you're sure you have no more changes to make to** @table **in the immediate 1098 * future.** 1099 * 1100 * If child level 0 page tables of @table also need to be flushed, this should 1101 * be done first using pvr_page_table_l0_sync() *before* calling this function. 1102 */ 1103 static void 1104 pvr_page_table_l1_sync(struct pvr_page_table_l1 *table) 1105 { 1106 pvr_mmu_backing_page_sync(&table->backing_page, PVR_MMU_SYNC_LEVEL_1_FLAGS); 1107 } 1108 1109 /** 1110 * pvr_page_table_l1_get_raw() - Access the raw equivalent of a mirror level 1 1111 * page table. 1112 * @table: Target level 1 page table. 1113 * 1114 * Essentially returns the CPU address of the raw equivalent of @table, cast to 1115 * a &struct pvr_page_table_l1_raw pointer. 1116 * 1117 * You probably want to call pvr_page_table_l1_get_entry_raw() instead. 1118 * 1119 * Return: 1120 * The raw equivalent of @table. 1121 */ 1122 static struct pvr_page_table_l1_raw * 1123 pvr_page_table_l1_get_raw(struct pvr_page_table_l1 *table) 1124 { 1125 return table->backing_page.host_ptr; 1126 } 1127 1128 /** 1129 * pvr_page_table_l1_get_entry_raw() - Access an entry from the raw equivalent 1130 * of a mirror level 1 page table. 1131 * @table: Target level 1 page table. 1132 * @idx: Index of the entry to access. 1133 * 1134 * Technically this function returns a pointer to a slot in a raw level 1 page 1135 * table, since the returned "entry" is not guaranteed to be valid. The caller 1136 * must verify the validity of the entry at the returned address (perhaps using 1137 * pvr_page_table_l1_entry_raw_is_valid()) before reading or overwriting it. 1138 * 1139 * The value of @idx is not checked here; it is the callers responsibility to 1140 * ensure @idx refers to a valid index within @table before dereferencing the 1141 * returned pointer. 1142 * 1143 * Return: 1144 * A pointer to the requested raw level 1 page table entry. 1145 */ 1146 static struct pvr_page_table_l1_entry_raw * 1147 pvr_page_table_l1_get_entry_raw(struct pvr_page_table_l1 *table, u16 idx) 1148 { 1149 return &pvr_page_table_l1_get_raw(table)->entries[idx]; 1150 } 1151 1152 /** 1153 * pvr_page_table_l1_entry_is_valid() - Check if a level 1 page table entry is 1154 * marked as valid. 1155 * @table: Target level 1 page table. 1156 * @idx: Index of the entry to check. 1157 * 1158 * The value of @idx is not checked here; it is the callers responsibility to 1159 * ensure @idx refers to a valid index within @table before calling this 1160 * function. 1161 */ 1162 static bool 1163 pvr_page_table_l1_entry_is_valid(struct pvr_page_table_l1 *table, u16 idx) 1164 { 1165 struct pvr_page_table_l1_entry_raw entry_raw = 1166 *pvr_page_table_l1_get_entry_raw(table, idx); 1167 1168 return pvr_page_table_l1_entry_raw_is_valid(entry_raw); 1169 } 1170 1171 /** 1172 * struct pvr_page_table_l0 - A wrapped level 0 page table. 1173 * 1174 * To access the raw part of this table, use pvr_page_table_l0_get_raw(). 1175 * Alternatively to access a raw entry directly, use 1176 * pvr_page_table_l0_get_entry_raw(). 1177 * 1178 * There is no mirror representation of an individual page, so this type has no 1179 * &entries member. 1180 */ 1181 struct pvr_page_table_l0 { 1182 /** 1183 * @backing_page: A handle to the memory which holds the raw 1184 * equivalent of this table. **For internal use only.** 1185 */ 1186 struct pvr_mmu_backing_page backing_page; 1187 1188 union { 1189 /** 1190 * @parent: The parent of this node in the page table tree structure. 1191 * 1192 * This is also a mirror table. 1193 * 1194 * Only valid when the L0 page table is active. When the L0 page table 1195 * has been removed and queued for destruction, the next_free field 1196 * should be used instead. 1197 */ 1198 struct pvr_page_table_l1 *parent; 1199 1200 /** 1201 * @next_free: Pointer to the next L0 page table to take/free. 1202 * 1203 * Used to form a linked list of L0 page tables. This is used 1204 * when preallocating tables and when the page table has been 1205 * removed and queued for destruction. 1206 */ 1207 struct pvr_page_table_l0 *next_free; 1208 }; 1209 1210 /** 1211 * @parent_idx: The index of the entry in the parent table (see 1212 * @parent) which corresponds to this table. 1213 */ 1214 u16 parent_idx; 1215 1216 /** 1217 * @entry_count: The current number of valid entries (that we know of) 1218 * in this table. This value is essentially a refcount - the table is 1219 * destroyed when this value is decremented to zero by 1220 * pvr_page_table_l0_remove(). 1221 */ 1222 u16 entry_count; 1223 }; 1224 1225 /** 1226 * pvr_page_table_l0_init() - Initialize a level 0 page table. 1227 * @table: Target level 0 page table. 1228 * @pvr_dev: Target PowerVR device 1229 * 1230 * When this function returns successfully, @table is still not considered 1231 * valid. It must be inserted into the page table tree structure with 1232 * pvr_page_table_l1_insert() before it is ready for use. 1233 * 1234 * It is expected that @table be zeroed (e.g. from kzalloc()) before calling 1235 * this function. 1236 * 1237 * Return: 1238 * * 0 on success, or 1239 * * Any error encountered while intializing &table->backing_page using 1240 * pvr_mmu_backing_page_init(). 1241 */ 1242 static int 1243 pvr_page_table_l0_init(struct pvr_page_table_l0 *table, 1244 struct pvr_device *pvr_dev) 1245 { 1246 table->parent_idx = PVR_IDX_INVALID; 1247 1248 return pvr_mmu_backing_page_init(&table->backing_page, pvr_dev); 1249 } 1250 1251 /** 1252 * pvr_page_table_l0_free() - Teardown a level 0 page table. 1253 * @table: Target level 0 page table. 1254 * 1255 * It is an error to attempt to use @table after calling this function, even 1256 * indirectly. This includes calling pvr_page_table_l1_remove(), which must 1257 * be called *before* pvr_page_table_l0_free(). 1258 */ 1259 static void 1260 pvr_page_table_l0_free(struct pvr_page_table_l0 *table) 1261 { 1262 pvr_mmu_backing_page_fini(&table->backing_page); 1263 kfree(table); 1264 } 1265 1266 /** 1267 * pvr_page_table_l0_sync() - Flush a level 0 page table from the CPU to the 1268 * device. 1269 * @table: Target level 0 page table. 1270 * 1271 * This is just a thin wrapper around pvr_mmu_backing_page_sync(), so the 1272 * warning there applies here too: **Only call pvr_page_table_l0_sync() once 1273 * you're sure you have no more changes to make to** @table **in the immediate 1274 * future.** 1275 * 1276 * If child pages of @table also need to be flushed, this should be done first 1277 * using a DMA sync function (e.g. dma_sync_sg_for_device()) *before* calling 1278 * this function. 1279 */ 1280 static void 1281 pvr_page_table_l0_sync(struct pvr_page_table_l0 *table) 1282 { 1283 pvr_mmu_backing_page_sync(&table->backing_page, PVR_MMU_SYNC_LEVEL_0_FLAGS); 1284 } 1285 1286 /** 1287 * pvr_page_table_l0_get_raw() - Access the raw equivalent of a mirror level 0 1288 * page table. 1289 * @table: Target level 0 page table. 1290 * 1291 * Essentially returns the CPU address of the raw equivalent of @table, cast to 1292 * a &struct pvr_page_table_l0_raw pointer. 1293 * 1294 * You probably want to call pvr_page_table_l0_get_entry_raw() instead. 1295 * 1296 * Return: 1297 * The raw equivalent of @table. 1298 */ 1299 static struct pvr_page_table_l0_raw * 1300 pvr_page_table_l0_get_raw(struct pvr_page_table_l0 *table) 1301 { 1302 return table->backing_page.host_ptr; 1303 } 1304 1305 /** 1306 * pvr_page_table_l0_get_entry_raw() - Access an entry from the raw equivalent 1307 * of a mirror level 0 page table. 1308 * @table: Target level 0 page table. 1309 * @idx: Index of the entry to access. 1310 * 1311 * Technically this function returns a pointer to a slot in a raw level 0 page 1312 * table, since the returned "entry" is not guaranteed to be valid. The caller 1313 * must verify the validity of the entry at the returned address (perhaps using 1314 * pvr_page_table_l0_entry_raw_is_valid()) before reading or overwriting it. 1315 * 1316 * The value of @idx is not checked here; it is the callers responsibility to 1317 * ensure @idx refers to a valid index within @table before dereferencing the 1318 * returned pointer. This is espcially important for level 0 page tables, which 1319 * can have a variable number of entries. 1320 * 1321 * Return: 1322 * A pointer to the requested raw level 0 page table entry. 1323 */ 1324 static struct pvr_page_table_l0_entry_raw * 1325 pvr_page_table_l0_get_entry_raw(struct pvr_page_table_l0 *table, u16 idx) 1326 { 1327 return &pvr_page_table_l0_get_raw(table)->entries[idx]; 1328 } 1329 1330 /** 1331 * pvr_page_table_l0_entry_is_valid() - Check if a level 0 page table entry is 1332 * marked as valid. 1333 * @table: Target level 0 page table. 1334 * @idx: Index of the entry to check. 1335 * 1336 * The value of @idx is not checked here; it is the callers responsibility to 1337 * ensure @idx refers to a valid index within @table before calling this 1338 * function. 1339 */ 1340 static bool 1341 pvr_page_table_l0_entry_is_valid(struct pvr_page_table_l0 *table, u16 idx) 1342 { 1343 struct pvr_page_table_l0_entry_raw entry_raw = 1344 *pvr_page_table_l0_get_entry_raw(table, idx); 1345 1346 return pvr_page_table_l0_entry_raw_is_valid(entry_raw); 1347 } 1348 1349 /** 1350 * struct pvr_mmu_context - context holding data for operations at page 1351 * catalogue level, intended for use with a VM context. 1352 */ 1353 struct pvr_mmu_context { 1354 /** @pvr_dev: The PVR device associated with the owning VM context. */ 1355 struct pvr_device *pvr_dev; 1356 1357 /** @page_table_l2: The MMU table root. */ 1358 struct pvr_page_table_l2 page_table_l2; 1359 }; 1360 1361 /** 1362 * struct pvr_page_table_ptr - A reference to a single physical page as indexed 1363 * by the page table structure. 1364 * 1365 * Intended for embedding in a &struct pvr_mmu_op_context. 1366 */ 1367 struct pvr_page_table_ptr { 1368 /** 1369 * @l1_table: A cached handle to the level 1 page table the 1370 * context is currently traversing. 1371 */ 1372 struct pvr_page_table_l1 *l1_table; 1373 1374 /** 1375 * @l0_table: A cached handle to the level 0 page table the 1376 * context is currently traversing. 1377 */ 1378 struct pvr_page_table_l0 *l0_table; 1379 1380 /** 1381 * @l2_idx: Index into the level 2 page table the context is 1382 * currently referencing. 1383 */ 1384 u16 l2_idx; 1385 1386 /** 1387 * @l1_idx: Index into the level 1 page table the context is 1388 * currently referencing. 1389 */ 1390 u16 l1_idx; 1391 1392 /** 1393 * @l0_idx: Index into the level 0 page table the context is 1394 * currently referencing. 1395 */ 1396 u16 l0_idx; 1397 }; 1398 1399 /** 1400 * struct pvr_mmu_op_context - context holding data for individual 1401 * device-virtual mapping operations. Intended for use with a VM bind operation. 1402 */ 1403 struct pvr_mmu_op_context { 1404 /** @mmu_ctx: The MMU context associated with the owning VM context. */ 1405 struct pvr_mmu_context *mmu_ctx; 1406 1407 /** @map: Data specifically for map operations. */ 1408 struct { 1409 /** 1410 * @sgt: Scatter gather table containing pages pinned for use by 1411 * this context - these are currently pinned when initialising 1412 * the VM bind operation. 1413 */ 1414 struct sg_table *sgt; 1415 1416 /** @sgt_offset: Start address of the device-virtual mapping. */ 1417 u64 sgt_offset; 1418 1419 /** 1420 * @l1_prealloc_tables: Preallocated l1 page table objects 1421 * use by this context when creating a page mapping. Linked list 1422 * fully created during initialisation. 1423 */ 1424 struct pvr_page_table_l1 *l1_prealloc_tables; 1425 1426 /** 1427 * @l0_prealloc_tables: Preallocated l0 page table objects 1428 * use by this context when creating a page mapping. Linked list 1429 * fully created during initialisation. 1430 */ 1431 struct pvr_page_table_l0 *l0_prealloc_tables; 1432 } map; 1433 1434 /** @unmap: Data specifically for unmap operations. */ 1435 struct { 1436 /** 1437 * @l1_free_tables: Collects page table objects freed by unmap 1438 * ops. Linked list empty at creation. 1439 */ 1440 struct pvr_page_table_l1 *l1_free_tables; 1441 1442 /** 1443 * @l0_free_tables: Collects page table objects freed by unmap 1444 * ops. Linked list empty at creation. 1445 */ 1446 struct pvr_page_table_l0 *l0_free_tables; 1447 } unmap; 1448 1449 /** 1450 * @curr_page: A reference to a single physical page as indexed by the 1451 * page table structure. 1452 */ 1453 struct pvr_page_table_ptr curr_page; 1454 1455 /** 1456 * @sync_level_required: The maximum level of the page table tree 1457 * structure which has (possibly) been modified since it was last 1458 * flushed to the device. 1459 * 1460 * This field should only be set with pvr_mmu_op_context_require_sync() 1461 * or indirectly by pvr_mmu_op_context_sync_partial(). 1462 */ 1463 enum pvr_mmu_sync_level sync_level_required; 1464 }; 1465 1466 /** 1467 * pvr_page_table_l2_insert() - Insert an entry referring to a level 1 page 1468 * table into a level 2 page table. 1469 * @op_ctx: Target MMU op context pointing at the entry to insert the L1 page 1470 * table into. 1471 * @child_table: Target level 1 page table to be referenced by the new entry. 1472 * 1473 * It is the caller's responsibility to ensure @op_ctx.curr_page points to a 1474 * valid L2 entry. 1475 * 1476 * It is the caller's responsibility to execute any memory barries to ensure 1477 * that the creation of @child_table is ordered before the L2 entry is inserted. 1478 */ 1479 static void 1480 pvr_page_table_l2_insert(struct pvr_mmu_op_context *op_ctx, 1481 struct pvr_page_table_l1 *child_table) 1482 { 1483 struct pvr_page_table_l2 *l2_table = 1484 &op_ctx->mmu_ctx->page_table_l2; 1485 struct pvr_page_table_l2_entry_raw *entry_raw = 1486 pvr_page_table_l2_get_entry_raw(l2_table, 1487 op_ctx->curr_page.l2_idx); 1488 1489 pvr_page_table_l2_entry_raw_set(entry_raw, 1490 child_table->backing_page.dma_addr); 1491 1492 child_table->parent = l2_table; 1493 child_table->parent_idx = op_ctx->curr_page.l2_idx; 1494 l2_table->entries[op_ctx->curr_page.l2_idx] = child_table; 1495 ++l2_table->entry_count; 1496 op_ctx->curr_page.l1_table = child_table; 1497 } 1498 1499 /** 1500 * pvr_page_table_l2_remove() - Remove a level 1 page table from a level 2 page 1501 * table. 1502 * @op_ctx: Target MMU op context pointing at the L2 entry to remove. 1503 * 1504 * It is the caller's responsibility to ensure @op_ctx.curr_page points to a 1505 * valid L2 entry. 1506 */ 1507 static void 1508 pvr_page_table_l2_remove(struct pvr_mmu_op_context *op_ctx) 1509 { 1510 struct pvr_page_table_l2 *l2_table = 1511 &op_ctx->mmu_ctx->page_table_l2; 1512 struct pvr_page_table_l2_entry_raw *entry_raw = 1513 pvr_page_table_l2_get_entry_raw(l2_table, 1514 op_ctx->curr_page.l1_table->parent_idx); 1515 1516 WARN_ON(op_ctx->curr_page.l1_table->parent != l2_table); 1517 1518 pvr_page_table_l2_entry_raw_clear(entry_raw); 1519 1520 l2_table->entries[op_ctx->curr_page.l1_table->parent_idx] = NULL; 1521 op_ctx->curr_page.l1_table->parent_idx = PVR_IDX_INVALID; 1522 op_ctx->curr_page.l1_table->next_free = op_ctx->unmap.l1_free_tables; 1523 op_ctx->unmap.l1_free_tables = op_ctx->curr_page.l1_table; 1524 op_ctx->curr_page.l1_table = NULL; 1525 1526 --l2_table->entry_count; 1527 } 1528 1529 /** 1530 * pvr_page_table_l1_insert() - Insert an entry referring to a level 0 page 1531 * table into a level 1 page table. 1532 * @op_ctx: Target MMU op context pointing at the entry to insert the L0 page 1533 * table into. 1534 * @child_table: L0 page table to insert. 1535 * 1536 * It is the caller's responsibility to ensure @op_ctx.curr_page points to a 1537 * valid L1 entry. 1538 * 1539 * It is the caller's responsibility to execute any memory barries to ensure 1540 * that the creation of @child_table is ordered before the L1 entry is inserted. 1541 */ 1542 static void 1543 pvr_page_table_l1_insert(struct pvr_mmu_op_context *op_ctx, 1544 struct pvr_page_table_l0 *child_table) 1545 { 1546 struct pvr_page_table_l1_entry_raw *entry_raw = 1547 pvr_page_table_l1_get_entry_raw(op_ctx->curr_page.l1_table, 1548 op_ctx->curr_page.l1_idx); 1549 1550 pvr_page_table_l1_entry_raw_set(entry_raw, 1551 child_table->backing_page.dma_addr); 1552 1553 child_table->parent = op_ctx->curr_page.l1_table; 1554 child_table->parent_idx = op_ctx->curr_page.l1_idx; 1555 op_ctx->curr_page.l1_table->entries[op_ctx->curr_page.l1_idx] = child_table; 1556 ++op_ctx->curr_page.l1_table->entry_count; 1557 op_ctx->curr_page.l0_table = child_table; 1558 } 1559 1560 /** 1561 * pvr_page_table_l1_remove() - Remove a level 0 page table from a level 1 page 1562 * table. 1563 * @op_ctx: Target MMU op context pointing at the L1 entry to remove. 1564 * 1565 * If this function results in the L1 table becoming empty, it will be removed 1566 * from its parent level 2 page table and destroyed. 1567 * 1568 * It is the caller's responsibility to ensure @op_ctx.curr_page points to a 1569 * valid L1 entry. 1570 */ 1571 static void 1572 pvr_page_table_l1_remove(struct pvr_mmu_op_context *op_ctx) 1573 { 1574 struct pvr_page_table_l1_entry_raw *entry_raw = 1575 pvr_page_table_l1_get_entry_raw(op_ctx->curr_page.l0_table->parent, 1576 op_ctx->curr_page.l0_table->parent_idx); 1577 1578 WARN_ON(op_ctx->curr_page.l0_table->parent != 1579 op_ctx->curr_page.l1_table); 1580 1581 pvr_page_table_l1_entry_raw_clear(entry_raw); 1582 1583 op_ctx->curr_page.l1_table->entries[op_ctx->curr_page.l0_table->parent_idx] = NULL; 1584 op_ctx->curr_page.l0_table->parent_idx = PVR_IDX_INVALID; 1585 op_ctx->curr_page.l0_table->next_free = op_ctx->unmap.l0_free_tables; 1586 op_ctx->unmap.l0_free_tables = op_ctx->curr_page.l0_table; 1587 op_ctx->curr_page.l0_table = NULL; 1588 1589 if (--op_ctx->curr_page.l1_table->entry_count == 0) { 1590 /* Clear the parent L2 page table entry. */ 1591 if (op_ctx->curr_page.l1_table->parent_idx != PVR_IDX_INVALID) 1592 pvr_page_table_l2_remove(op_ctx); 1593 } 1594 } 1595 1596 /** 1597 * pvr_page_table_l0_insert() - Insert an entry referring to a physical page 1598 * into a level 0 page table. 1599 * @op_ctx: Target MMU op context pointing at the L0 entry to insert. 1600 * @dma_addr: Target DMA address to be referenced by the new entry. 1601 * @flags: Page options to be stored in the new entry. 1602 * 1603 * It is the caller's responsibility to ensure @op_ctx.curr_page points to a 1604 * valid L0 entry. 1605 */ 1606 static void 1607 pvr_page_table_l0_insert(struct pvr_mmu_op_context *op_ctx, 1608 dma_addr_t dma_addr, struct pvr_page_flags_raw flags) 1609 { 1610 struct pvr_page_table_l0_entry_raw *entry_raw = 1611 pvr_page_table_l0_get_entry_raw(op_ctx->curr_page.l0_table, 1612 op_ctx->curr_page.l0_idx); 1613 1614 pvr_page_table_l0_entry_raw_set(entry_raw, dma_addr, flags); 1615 1616 /* 1617 * There is no entry to set here - we don't keep a mirror of 1618 * individual pages. 1619 */ 1620 1621 ++op_ctx->curr_page.l0_table->entry_count; 1622 } 1623 1624 /** 1625 * pvr_page_table_l0_remove() - Remove a physical page from a level 0 page 1626 * table. 1627 * @op_ctx: Target MMU op context pointing at the L0 entry to remove. 1628 * 1629 * If this function results in the L0 table becoming empty, it will be removed 1630 * from its parent L1 page table and destroyed. 1631 * 1632 * It is the caller's responsibility to ensure @op_ctx.curr_page points to a 1633 * valid L0 entry. 1634 */ 1635 static void 1636 pvr_page_table_l0_remove(struct pvr_mmu_op_context *op_ctx) 1637 { 1638 struct pvr_page_table_l0_entry_raw *entry_raw = 1639 pvr_page_table_l0_get_entry_raw(op_ctx->curr_page.l0_table, 1640 op_ctx->curr_page.l0_idx); 1641 1642 pvr_page_table_l0_entry_raw_clear(entry_raw); 1643 1644 /* 1645 * There is no entry to clear here - we don't keep a mirror of 1646 * individual pages. 1647 */ 1648 1649 if (--op_ctx->curr_page.l0_table->entry_count == 0) { 1650 /* Clear the parent L1 page table entry. */ 1651 if (op_ctx->curr_page.l0_table->parent_idx != PVR_IDX_INVALID) 1652 pvr_page_table_l1_remove(op_ctx); 1653 } 1654 } 1655 1656 /** 1657 * DOC: Page table index utilities 1658 */ 1659 1660 /** 1661 * pvr_page_table_l2_idx() - Calculate the level 2 page table index for a 1662 * device-virtual address. 1663 * @device_addr: Target device-virtual address. 1664 * 1665 * This function does not perform any bounds checking - it is the caller's 1666 * responsibility to ensure that @device_addr is valid before interpreting 1667 * the result. 1668 * 1669 * Return: 1670 * The index into a level 2 page table corresponding to @device_addr. 1671 */ 1672 static u16 1673 pvr_page_table_l2_idx(u64 device_addr) 1674 { 1675 return (device_addr & ~ROGUE_MMUCTRL_VADDR_PC_INDEX_CLRMSK) >> 1676 ROGUE_MMUCTRL_VADDR_PC_INDEX_SHIFT; 1677 } 1678 1679 /** 1680 * pvr_page_table_l1_idx() - Calculate the level 1 page table index for a 1681 * device-virtual address. 1682 * @device_addr: Target device-virtual address. 1683 * 1684 * This function does not perform any bounds checking - it is the caller's 1685 * responsibility to ensure that @device_addr is valid before interpreting 1686 * the result. 1687 * 1688 * Return: 1689 * The index into a level 1 page table corresponding to @device_addr. 1690 */ 1691 static u16 1692 pvr_page_table_l1_idx(u64 device_addr) 1693 { 1694 return (device_addr & ~ROGUE_MMUCTRL_VADDR_PD_INDEX_CLRMSK) >> 1695 ROGUE_MMUCTRL_VADDR_PD_INDEX_SHIFT; 1696 } 1697 1698 /** 1699 * pvr_page_table_l0_idx() - Calculate the level 0 page table index for a 1700 * device-virtual address. 1701 * @device_addr: Target device-virtual address. 1702 * 1703 * This function does not perform any bounds checking - it is the caller's 1704 * responsibility to ensure that @device_addr is valid before interpreting 1705 * the result. 1706 * 1707 * Return: 1708 * The index into a level 0 page table corresponding to @device_addr. 1709 */ 1710 static u16 1711 pvr_page_table_l0_idx(u64 device_addr) 1712 { 1713 return (device_addr & ~ROGUE_MMUCTRL_VADDR_PT_INDEX_CLRMSK) >> 1714 ROGUE_MMUCTRL_PAGE_X_RANGE_SHIFT; 1715 } 1716 1717 /** 1718 * DOC: High-level page table operations 1719 */ 1720 1721 /** 1722 * pvr_page_table_l1_get_or_insert() - Retrieves (optionally inserting if 1723 * necessary) a level 1 page table from the specified level 2 page table entry. 1724 * @op_ctx: Target MMU op context. 1725 * @should_insert: [IN] Specifies whether new page tables should be inserted 1726 * when empty page table entries are encountered during traversal. 1727 * 1728 * Return: 1729 * * 0 on success, or 1730 * 1731 * If @should_insert is %false: 1732 * * -%ENXIO if a level 1 page table would have been inserted. 1733 * 1734 * If @should_insert is %true: 1735 * * Any error encountered while inserting the level 1 page table. 1736 */ 1737 static int 1738 pvr_page_table_l1_get_or_insert(struct pvr_mmu_op_context *op_ctx, 1739 bool should_insert) 1740 { 1741 struct pvr_page_table_l2 *l2_table = 1742 &op_ctx->mmu_ctx->page_table_l2; 1743 struct pvr_page_table_l1 *table; 1744 1745 if (pvr_page_table_l2_entry_is_valid(l2_table, 1746 op_ctx->curr_page.l2_idx)) { 1747 op_ctx->curr_page.l1_table = 1748 l2_table->entries[op_ctx->curr_page.l2_idx]; 1749 return 0; 1750 } 1751 1752 if (!should_insert) 1753 return -ENXIO; 1754 1755 /* Take a prealloced table. */ 1756 table = op_ctx->map.l1_prealloc_tables; 1757 if (!table) 1758 return -ENOMEM; 1759 1760 /* Pop */ 1761 op_ctx->map.l1_prealloc_tables = table->next_free; 1762 table->next_free = NULL; 1763 1764 /* Ensure new table is fully written out before adding to L2 page table. */ 1765 wmb(); 1766 1767 pvr_page_table_l2_insert(op_ctx, table); 1768 1769 return 0; 1770 } 1771 1772 /** 1773 * pvr_page_table_l0_get_or_insert() - Retrieves (optionally inserting if 1774 * necessary) a level 0 page table from the specified level 1 page table entry. 1775 * @op_ctx: Target MMU op context. 1776 * @should_insert: [IN] Specifies whether new page tables should be inserted 1777 * when empty page table entries are encountered during traversal. 1778 * 1779 * Return: 1780 * * 0 on success, 1781 * 1782 * If @should_insert is %false: 1783 * * -%ENXIO if a level 0 page table would have been inserted. 1784 * 1785 * If @should_insert is %true: 1786 * * Any error encountered while inserting the level 0 page table. 1787 */ 1788 static int 1789 pvr_page_table_l0_get_or_insert(struct pvr_mmu_op_context *op_ctx, 1790 bool should_insert) 1791 { 1792 struct pvr_page_table_l0 *table; 1793 1794 if (pvr_page_table_l1_entry_is_valid(op_ctx->curr_page.l1_table, 1795 op_ctx->curr_page.l1_idx)) { 1796 op_ctx->curr_page.l0_table = 1797 op_ctx->curr_page.l1_table->entries[op_ctx->curr_page.l1_idx]; 1798 return 0; 1799 } 1800 1801 if (!should_insert) 1802 return -ENXIO; 1803 1804 /* Take a prealloced table. */ 1805 table = op_ctx->map.l0_prealloc_tables; 1806 if (!table) 1807 return -ENOMEM; 1808 1809 /* Pop */ 1810 op_ctx->map.l0_prealloc_tables = table->next_free; 1811 table->next_free = NULL; 1812 1813 /* Ensure new table is fully written out before adding to L1 page table. */ 1814 wmb(); 1815 1816 pvr_page_table_l1_insert(op_ctx, table); 1817 1818 return 0; 1819 } 1820 1821 /** 1822 * pvr_mmu_context_create() - Create an MMU context. 1823 * @pvr_dev: PVR device associated with owning VM context. 1824 * 1825 * Returns: 1826 * * Newly created MMU context object on success, or 1827 * * -%ENOMEM if no memory is available, 1828 * * Any error code returned by pvr_page_table_l2_init(). 1829 */ 1830 struct pvr_mmu_context *pvr_mmu_context_create(struct pvr_device *pvr_dev) 1831 { 1832 struct pvr_mmu_context *ctx = kzalloc_obj(*ctx); 1833 int err; 1834 1835 if (!ctx) 1836 return ERR_PTR(-ENOMEM); 1837 1838 err = pvr_page_table_l2_init(&ctx->page_table_l2, pvr_dev); 1839 if (err) 1840 return ERR_PTR(err); 1841 1842 ctx->pvr_dev = pvr_dev; 1843 1844 return ctx; 1845 } 1846 1847 /** 1848 * pvr_mmu_context_destroy() - Destroy an MMU context. 1849 * @ctx: Target MMU context. 1850 */ 1851 void pvr_mmu_context_destroy(struct pvr_mmu_context *ctx) 1852 { 1853 pvr_page_table_l2_fini(&ctx->page_table_l2); 1854 kfree(ctx); 1855 } 1856 1857 /** 1858 * pvr_mmu_get_root_table_dma_addr() - Get the DMA address of the root of the 1859 * page table structure behind a VM context. 1860 * @ctx: Target MMU context. 1861 */ 1862 dma_addr_t pvr_mmu_get_root_table_dma_addr(struct pvr_mmu_context *ctx) 1863 { 1864 return ctx->page_table_l2.backing_page.dma_addr; 1865 } 1866 1867 /** 1868 * pvr_page_table_l1_alloc() - Allocate a l1 page_table object. 1869 * @ctx: MMU context of owning VM context. 1870 * 1871 * Returns: 1872 * * Newly created page table object on success, or 1873 * * -%ENOMEM if no memory is available, 1874 * * Any error code returned by pvr_page_table_l1_init(). 1875 */ 1876 static struct pvr_page_table_l1 * 1877 pvr_page_table_l1_alloc(struct pvr_mmu_context *ctx) 1878 { 1879 int err; 1880 1881 struct pvr_page_table_l1 *table = kzalloc_obj(*table); 1882 1883 if (!table) 1884 return ERR_PTR(-ENOMEM); 1885 1886 err = pvr_page_table_l1_init(table, ctx->pvr_dev); 1887 if (err) { 1888 kfree(table); 1889 return ERR_PTR(err); 1890 } 1891 1892 return table; 1893 } 1894 1895 /** 1896 * pvr_page_table_l0_alloc() - Allocate a l0 page_table object. 1897 * @ctx: MMU context of owning VM context. 1898 * 1899 * Returns: 1900 * * Newly created page table object on success, or 1901 * * -%ENOMEM if no memory is available, 1902 * * Any error code returned by pvr_page_table_l0_init(). 1903 */ 1904 static struct pvr_page_table_l0 * 1905 pvr_page_table_l0_alloc(struct pvr_mmu_context *ctx) 1906 { 1907 int err; 1908 1909 struct pvr_page_table_l0 *table = kzalloc_obj(*table); 1910 1911 if (!table) 1912 return ERR_PTR(-ENOMEM); 1913 1914 err = pvr_page_table_l0_init(table, ctx->pvr_dev); 1915 if (err) { 1916 kfree(table); 1917 return ERR_PTR(err); 1918 } 1919 1920 return table; 1921 } 1922 1923 /** 1924 * pvr_mmu_op_context_require_sync() - Mark an MMU op context as requiring a 1925 * sync operation for the referenced page tables up to a specified level. 1926 * @op_ctx: Target MMU op context. 1927 * @level: Maximum page table level for which a sync is required. 1928 */ 1929 static void 1930 pvr_mmu_op_context_require_sync(struct pvr_mmu_op_context *op_ctx, 1931 enum pvr_mmu_sync_level level) 1932 { 1933 if (op_ctx->sync_level_required < level) 1934 op_ctx->sync_level_required = level; 1935 } 1936 1937 /** 1938 * pvr_mmu_op_context_sync_manual() - Trigger a sync of some or all of the 1939 * page tables referenced by a MMU op context. 1940 * @op_ctx: Target MMU op context. 1941 * @level: Maximum page table level to sync. 1942 * 1943 * Do not call this function directly. Instead use 1944 * pvr_mmu_op_context_sync_partial() which is checked against the current 1945 * value of &op_ctx->sync_level_required as set by 1946 * pvr_mmu_op_context_require_sync(). 1947 */ 1948 static void 1949 pvr_mmu_op_context_sync_manual(struct pvr_mmu_op_context *op_ctx, 1950 enum pvr_mmu_sync_level level) 1951 { 1952 /* 1953 * We sync the page table levels in ascending order (starting from the 1954 * leaf node) to ensure consistency. 1955 */ 1956 1957 WARN_ON(level < PVR_MMU_SYNC_LEVEL_NONE); 1958 1959 if (level <= PVR_MMU_SYNC_LEVEL_NONE) 1960 return; 1961 1962 if (op_ctx->curr_page.l0_table) 1963 pvr_page_table_l0_sync(op_ctx->curr_page.l0_table); 1964 1965 if (level < PVR_MMU_SYNC_LEVEL_1) 1966 return; 1967 1968 if (op_ctx->curr_page.l1_table) 1969 pvr_page_table_l1_sync(op_ctx->curr_page.l1_table); 1970 1971 if (level < PVR_MMU_SYNC_LEVEL_2) 1972 return; 1973 1974 pvr_page_table_l2_sync(&op_ctx->mmu_ctx->page_table_l2); 1975 } 1976 1977 /** 1978 * pvr_mmu_op_context_sync_partial() - Trigger a sync of some or all of the 1979 * page tables referenced by a MMU op context. 1980 * @op_ctx: Target MMU op context. 1981 * @level: Requested page table level to sync up to (inclusive). 1982 * 1983 * If @level is greater than the maximum level recorded by @op_ctx as requiring 1984 * a sync operation, only the previously recorded maximum will be used. 1985 * 1986 * Additionally, if @level is greater than or equal to the maximum level 1987 * recorded by @op_ctx as requiring a sync operation, that maximum level will be 1988 * reset as a full sync will be performed. This is equivalent to calling 1989 * pvr_mmu_op_context_sync(). 1990 */ 1991 static void 1992 pvr_mmu_op_context_sync_partial(struct pvr_mmu_op_context *op_ctx, 1993 enum pvr_mmu_sync_level level) 1994 { 1995 /* 1996 * If the requested sync level is greater than or equal to the 1997 * currently required sync level, we do two things: 1998 * * Don't waste time syncing levels we haven't previously marked as 1999 * requiring a sync, and 2000 * * Reset the required sync level since we are about to sync 2001 * everything that was previously marked as requiring a sync. 2002 */ 2003 if (level >= op_ctx->sync_level_required) { 2004 level = op_ctx->sync_level_required; 2005 op_ctx->sync_level_required = PVR_MMU_SYNC_LEVEL_NONE; 2006 } 2007 2008 pvr_mmu_op_context_sync_manual(op_ctx, level); 2009 } 2010 2011 /** 2012 * pvr_mmu_op_context_sync() - Trigger a sync of every page table referenced by 2013 * a MMU op context. 2014 * @op_ctx: Target MMU op context. 2015 * 2016 * The maximum level marked internally as requiring a sync will be reset so 2017 * that subsequent calls to this function will be no-ops unless @op_ctx is 2018 * otherwise updated. 2019 */ 2020 static void 2021 pvr_mmu_op_context_sync(struct pvr_mmu_op_context *op_ctx) 2022 { 2023 pvr_mmu_op_context_sync_manual(op_ctx, op_ctx->sync_level_required); 2024 2025 op_ctx->sync_level_required = PVR_MMU_SYNC_LEVEL_NONE; 2026 } 2027 2028 /** 2029 * pvr_mmu_op_context_load_tables() - Load pointers to tables in each level of 2030 * the page table tree structure needed to reference the physical page 2031 * referenced by a MMU op context. 2032 * @op_ctx: Target MMU op context. 2033 * @should_create: Specifies whether new page tables should be created when 2034 * empty page table entries are encountered during traversal. 2035 * @load_level_required: Maximum page table level to load. 2036 * 2037 * If @should_create is %true, this function may modify the stored required 2038 * sync level of @op_ctx as new page tables are created and inserted into their 2039 * respective parents. 2040 * 2041 * Since there is only one root page table, it is technically incorrect to call 2042 * this function with a value of @load_level_required greater than or equal to 2043 * the root level number. However, this is not explicitly disallowed here. 2044 * 2045 * Return: 2046 * * 0 on success, 2047 * * Any error returned by pvr_page_table_l1_get_or_create() if 2048 * @load_level_required >= 1 except -%ENXIO, or 2049 * * Any error returned by pvr_page_table_l0_get_or_create() if 2050 * @load_level_required >= 0 except -%ENXIO. 2051 */ 2052 static int 2053 pvr_mmu_op_context_load_tables(struct pvr_mmu_op_context *op_ctx, 2054 bool should_create, 2055 enum pvr_mmu_sync_level load_level_required) 2056 { 2057 const struct pvr_page_table_l1 *l1_head_before = 2058 op_ctx->map.l1_prealloc_tables; 2059 const struct pvr_page_table_l0 *l0_head_before = 2060 op_ctx->map.l0_prealloc_tables; 2061 int err; 2062 2063 /* Clear tables we're about to fetch in case of error states. */ 2064 if (load_level_required >= PVR_MMU_SYNC_LEVEL_1) 2065 op_ctx->curr_page.l1_table = NULL; 2066 2067 if (load_level_required >= PVR_MMU_SYNC_LEVEL_0) 2068 op_ctx->curr_page.l0_table = NULL; 2069 2070 /* Get or create L1 page table. */ 2071 if (load_level_required >= PVR_MMU_SYNC_LEVEL_1) { 2072 err = pvr_page_table_l1_get_or_insert(op_ctx, should_create); 2073 if (err) { 2074 /* 2075 * If @should_create is %false and no L1 page table was 2076 * found, return early but without an error. Since 2077 * pvr_page_table_l1_get_or_create() can only return 2078 * -%ENXIO if @should_create is %false, there is no 2079 * need to check it here. 2080 */ 2081 if (err == -ENXIO) 2082 err = 0; 2083 2084 return err; 2085 } 2086 } 2087 2088 /* Get or create L0 page table. */ 2089 if (load_level_required >= PVR_MMU_SYNC_LEVEL_0) { 2090 err = pvr_page_table_l0_get_or_insert(op_ctx, should_create); 2091 if (err) { 2092 /* 2093 * If @should_create is %false and no L0 page table was 2094 * found, return early but without an error. Since 2095 * pvr_page_table_l0_get_or_insert() can only return 2096 * -%ENXIO if @should_create is %false, there is no 2097 * need to check it here. 2098 */ 2099 if (err == -ENXIO) 2100 err = 0; 2101 2102 /* 2103 * At this point, an L1 page table could have been 2104 * inserted but is now empty due to the failed attempt 2105 * at inserting an L0 page table. In this instance, we 2106 * must remove the empty L1 page table ourselves as 2107 * pvr_page_table_l1_remove() is never called as part 2108 * of the error path in 2109 * pvr_page_table_l0_get_or_insert(). 2110 */ 2111 if (l1_head_before != op_ctx->map.l1_prealloc_tables) { 2112 pvr_page_table_l2_remove(op_ctx); 2113 pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_2); 2114 } 2115 2116 return err; 2117 } 2118 } 2119 2120 /* 2121 * A sync is only needed if table objects were inserted. This can be 2122 * inferred by checking if the pointer at the head of the linked list 2123 * has changed. 2124 */ 2125 if (l1_head_before != op_ctx->map.l1_prealloc_tables) 2126 pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_2); 2127 else if (l0_head_before != op_ctx->map.l0_prealloc_tables) 2128 pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_1); 2129 2130 return 0; 2131 } 2132 2133 /** 2134 * pvr_mmu_op_context_set_curr_page() - Reassign the current page of an MMU op 2135 * context, syncing any page tables previously assigned to it which are no 2136 * longer relevant. 2137 * @op_ctx: Target MMU op context. 2138 * @device_addr: New pointer target. 2139 * @should_create: Specify whether new page tables should be created when 2140 * empty page table entries are encountered during traversal. 2141 * 2142 * This function performs a full sync on the pointer, regardless of which 2143 * levels are modified. 2144 * 2145 * Return: 2146 * * 0 on success, or 2147 * * Any error returned by pvr_mmu_op_context_load_tables(). 2148 */ 2149 static int 2150 pvr_mmu_op_context_set_curr_page(struct pvr_mmu_op_context *op_ctx, 2151 u64 device_addr, bool should_create) 2152 { 2153 pvr_mmu_op_context_sync(op_ctx); 2154 2155 op_ctx->curr_page.l2_idx = pvr_page_table_l2_idx(device_addr); 2156 op_ctx->curr_page.l1_idx = pvr_page_table_l1_idx(device_addr); 2157 op_ctx->curr_page.l0_idx = pvr_page_table_l0_idx(device_addr); 2158 op_ctx->curr_page.l1_table = NULL; 2159 op_ctx->curr_page.l0_table = NULL; 2160 2161 return pvr_mmu_op_context_load_tables(op_ctx, should_create, 2162 PVR_MMU_SYNC_LEVEL_1); 2163 } 2164 2165 /** 2166 * pvr_mmu_op_context_next_page() - Advance the current page of an MMU op 2167 * context. 2168 * @op_ctx: Target MMU op context. 2169 * @should_create: Specify whether new page tables should be created when 2170 * empty page table entries are encountered during traversal. 2171 * 2172 * If @should_create is %false, it is the caller's responsibility to verify that 2173 * the state of the table references in @op_ctx is valid on return. If -%ENXIO 2174 * is returned, at least one of the table references is invalid. It should be 2175 * noted that @op_ctx as a whole will be left in a valid state if -%ENXIO is 2176 * returned, unlike other error codes. The caller should check which references 2177 * are invalid by comparing them to %NULL. Only &@ptr->l2_table is guaranteed 2178 * to be valid, since it represents the root of the page table tree structure. 2179 * 2180 * Return: 2181 * * 0 on success, 2182 * * -%EPERM if the operation would wrap at the top of the page table 2183 * hierarchy, 2184 * * -%ENXIO if @should_create is %false and a page table of any level would 2185 * have otherwise been created, or 2186 * * Any error returned while attempting to create missing page tables if 2187 * @should_create is %true. 2188 */ 2189 static int 2190 pvr_mmu_op_context_next_page(struct pvr_mmu_op_context *op_ctx, 2191 bool should_create) 2192 { 2193 s8 load_level_required = PVR_MMU_SYNC_LEVEL_NONE; 2194 2195 if (++op_ctx->curr_page.l0_idx != ROGUE_MMUCTRL_ENTRIES_PT_VALUE_X) 2196 goto load_tables; 2197 2198 op_ctx->curr_page.l0_idx = 0; 2199 load_level_required = PVR_MMU_SYNC_LEVEL_0; 2200 2201 if (++op_ctx->curr_page.l1_idx != ROGUE_MMUCTRL_ENTRIES_PD_VALUE) 2202 goto load_tables; 2203 2204 op_ctx->curr_page.l1_idx = 0; 2205 load_level_required = PVR_MMU_SYNC_LEVEL_1; 2206 2207 if (++op_ctx->curr_page.l2_idx != ROGUE_MMUCTRL_ENTRIES_PC_VALUE) 2208 goto load_tables; 2209 2210 /* 2211 * If the pattern continued, we would set &op_ctx->curr_page.l2_idx to 2212 * zero here. However, that would wrap the top layer of the page table 2213 * hierarchy which is not a valid operation. Instead, we warn and return 2214 * an error. 2215 */ 2216 WARN(true, 2217 "%s(%p) attempted to loop the top of the page table hierarchy", 2218 __func__, op_ctx); 2219 return -EPERM; 2220 2221 /* If indices have wrapped, we need to load new tables. */ 2222 load_tables: 2223 /* First, flush tables which will be unloaded. */ 2224 pvr_mmu_op_context_sync_partial(op_ctx, load_level_required); 2225 2226 /* Then load tables from the required level down. */ 2227 return pvr_mmu_op_context_load_tables(op_ctx, should_create, 2228 load_level_required); 2229 } 2230 2231 /** 2232 * DOC: Single page operations 2233 */ 2234 2235 /** 2236 * pvr_page_create() - Create a device-virtual memory page and insert it into 2237 * a level 0 page table. 2238 * @op_ctx: Target MMU op context pointing at the device-virtual address of the 2239 * target page. 2240 * @dma_addr: DMA address of the physical page backing the created page. 2241 * @flags: Page options saved on the level 0 page table entry for reading by 2242 * the device. 2243 * 2244 * Return: 2245 * * 0 on success, or 2246 * * -%EEXIST if the requested page already exists. 2247 */ 2248 static int 2249 pvr_page_create(struct pvr_mmu_op_context *op_ctx, dma_addr_t dma_addr, 2250 struct pvr_page_flags_raw flags) 2251 { 2252 /* Do not create a new page if one already exists. */ 2253 if (pvr_page_table_l0_entry_is_valid(op_ctx->curr_page.l0_table, 2254 op_ctx->curr_page.l0_idx)) { 2255 return -EEXIST; 2256 } 2257 2258 pvr_page_table_l0_insert(op_ctx, dma_addr, flags); 2259 2260 pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_0); 2261 2262 return 0; 2263 } 2264 2265 /** 2266 * pvr_page_destroy() - Destroy a device page after removing it from its 2267 * parent level 0 page table. 2268 * @op_ctx: Target MMU op context. 2269 */ 2270 static void 2271 pvr_page_destroy(struct pvr_mmu_op_context *op_ctx) 2272 { 2273 /* Do nothing if the page does not exist. */ 2274 if (!pvr_page_table_l0_entry_is_valid(op_ctx->curr_page.l0_table, 2275 op_ctx->curr_page.l0_idx)) { 2276 return; 2277 } 2278 2279 /* Clear the parent L0 page table entry. */ 2280 pvr_page_table_l0_remove(op_ctx); 2281 2282 pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_0); 2283 } 2284 2285 /** 2286 * pvr_mmu_op_context_destroy() - Destroy an MMU op context. 2287 * @op_ctx: Target MMU op context. 2288 */ 2289 void pvr_mmu_op_context_destroy(struct pvr_mmu_op_context *op_ctx) 2290 { 2291 const bool flush_caches = 2292 op_ctx->sync_level_required != PVR_MMU_SYNC_LEVEL_NONE; 2293 2294 pvr_mmu_op_context_sync(op_ctx); 2295 2296 /* Unmaps should be flushed immediately. Map flushes can be deferred. */ 2297 if (flush_caches && !op_ctx->map.sgt) 2298 pvr_mmu_flush_exec(op_ctx->mmu_ctx->pvr_dev, true); 2299 2300 while (op_ctx->map.l0_prealloc_tables) { 2301 struct pvr_page_table_l0 *tmp = op_ctx->map.l0_prealloc_tables; 2302 2303 op_ctx->map.l0_prealloc_tables = 2304 op_ctx->map.l0_prealloc_tables->next_free; 2305 pvr_page_table_l0_free(tmp); 2306 } 2307 2308 while (op_ctx->map.l1_prealloc_tables) { 2309 struct pvr_page_table_l1 *tmp = op_ctx->map.l1_prealloc_tables; 2310 2311 op_ctx->map.l1_prealloc_tables = 2312 op_ctx->map.l1_prealloc_tables->next_free; 2313 pvr_page_table_l1_free(tmp); 2314 } 2315 2316 while (op_ctx->unmap.l0_free_tables) { 2317 struct pvr_page_table_l0 *tmp = op_ctx->unmap.l0_free_tables; 2318 2319 op_ctx->unmap.l0_free_tables = 2320 op_ctx->unmap.l0_free_tables->next_free; 2321 pvr_page_table_l0_free(tmp); 2322 } 2323 2324 while (op_ctx->unmap.l1_free_tables) { 2325 struct pvr_page_table_l1 *tmp = op_ctx->unmap.l1_free_tables; 2326 2327 op_ctx->unmap.l1_free_tables = 2328 op_ctx->unmap.l1_free_tables->next_free; 2329 pvr_page_table_l1_free(tmp); 2330 } 2331 2332 kfree(op_ctx); 2333 } 2334 2335 /** 2336 * pvr_mmu_op_context_create() - Create an MMU op context. 2337 * @ctx: MMU context associated with owning VM context. 2338 * @sgt: Scatter gather table containing pages pinned for use by this context. 2339 * @device_addr: Virtual device address at the start of the requested mapping. 2340 * @sgt_offset: Offset into @sgt of the start of the requested mapping. 2341 * @size: Size in bytes of the requested device-virtual memory mapping. For an 2342 * unmapping, this should be zero so that no page tables are allocated. 2343 * 2344 * Returns: 2345 * * Newly created MMU op context object on success, or 2346 * * -%ENOMEM if no memory is available, 2347 * * Any error code returned by pvr_page_table_l2_init(). 2348 */ 2349 struct pvr_mmu_op_context * 2350 pvr_mmu_op_context_create(struct pvr_mmu_context *ctx, struct sg_table *sgt, 2351 u64 device_addr, u64 sgt_offset, u64 size) 2352 { 2353 int err; 2354 2355 struct pvr_mmu_op_context *op_ctx = kzalloc_obj(*op_ctx); 2356 2357 if (!op_ctx) 2358 return ERR_PTR(-ENOMEM); 2359 2360 op_ctx->mmu_ctx = ctx; 2361 op_ctx->map.sgt = sgt; 2362 op_ctx->map.sgt_offset = sgt_offset; 2363 op_ctx->sync_level_required = PVR_MMU_SYNC_LEVEL_NONE; 2364 2365 if (size) { 2366 /* 2367 * The page tables needed are set by the device-virtual range 2368 * being mapped: one level 1 table per 1GiB region and one 2369 * level 0 table per 2MiB region the range touches. Tables that 2370 * already exist are not consumed, so this is an upper bound. 2371 */ 2372 const u64 last_addr = device_addr + size - 1; 2373 const u64 l1_count = 2374 (last_addr >> ROGUE_MMUCTRL_VADDR_PC_INDEX_SHIFT) - 2375 (device_addr >> ROGUE_MMUCTRL_VADDR_PC_INDEX_SHIFT) + 1; 2376 const u64 l0_count = 2377 (last_addr >> ROGUE_MMUCTRL_VADDR_PD_INDEX_SHIFT) - 2378 (device_addr >> ROGUE_MMUCTRL_VADDR_PD_INDEX_SHIFT) + 1; 2379 2380 /* 2381 * Alloc and push page table entries until we have enough of 2382 * each type, ending with linked lists of l0 and l1 entries in 2383 * reverse order. 2384 */ 2385 for (int i = 0; i < l1_count; i++) { 2386 struct pvr_page_table_l1 *l1_tmp = 2387 pvr_page_table_l1_alloc(ctx); 2388 2389 err = PTR_ERR_OR_ZERO(l1_tmp); 2390 if (err) 2391 goto err_cleanup; 2392 2393 l1_tmp->next_free = op_ctx->map.l1_prealloc_tables; 2394 op_ctx->map.l1_prealloc_tables = l1_tmp; 2395 } 2396 2397 for (int i = 0; i < l0_count; i++) { 2398 struct pvr_page_table_l0 *l0_tmp = 2399 pvr_page_table_l0_alloc(ctx); 2400 2401 err = PTR_ERR_OR_ZERO(l0_tmp); 2402 if (err) 2403 goto err_cleanup; 2404 2405 l0_tmp->next_free = op_ctx->map.l0_prealloc_tables; 2406 op_ctx->map.l0_prealloc_tables = l0_tmp; 2407 } 2408 } 2409 2410 return op_ctx; 2411 2412 err_cleanup: 2413 pvr_mmu_op_context_destroy(op_ctx); 2414 2415 return ERR_PTR(err); 2416 } 2417 2418 /** 2419 * pvr_mmu_op_context_unmap_curr_page() - Unmap pages from a memory context 2420 * starting from the current page of an MMU op context. 2421 * @op_ctx: Target MMU op context pointing at the first page to unmap. 2422 * @nr_pages: Number of pages to unmap. 2423 * 2424 * Return: 2425 * * 0 on success, or 2426 * * Any error encountered while advancing @op_ctx.curr_page with 2427 * pvr_mmu_op_context_next_page() (except -%ENXIO). 2428 */ 2429 static int 2430 pvr_mmu_op_context_unmap_curr_page(struct pvr_mmu_op_context *op_ctx, 2431 u64 nr_pages) 2432 { 2433 int err; 2434 2435 if (nr_pages == 0) 2436 return 0; 2437 2438 /* 2439 * Destroy first page outside loop, as it doesn't require a page 2440 * advance beforehand. If the L0 page table reference in 2441 * @op_ctx.curr_page is %NULL, there cannot be a mapped page at 2442 * @op_ctx.curr_page (so skip ahead). 2443 */ 2444 if (op_ctx->curr_page.l0_table) 2445 pvr_page_destroy(op_ctx); 2446 2447 for (u64 page = 1; page < nr_pages; ++page) { 2448 err = pvr_mmu_op_context_next_page(op_ctx, false); 2449 /* 2450 * If the page table tree structure at @op_ctx.curr_page is 2451 * incomplete, skip ahead. We don't care about unmapping pages 2452 * that cannot exist. 2453 * 2454 * FIXME: This could be made more efficient by jumping ahead 2455 * using pvr_mmu_op_context_set_curr_page(). 2456 */ 2457 if (err == -ENXIO) 2458 continue; 2459 else if (err) 2460 return err; 2461 2462 pvr_page_destroy(op_ctx); 2463 } 2464 2465 return 0; 2466 } 2467 2468 /** 2469 * pvr_mmu_unmap() - Unmap pages from a memory context. 2470 * @op_ctx: Target MMU op context. 2471 * @device_addr: First device-virtual address to unmap. 2472 * @size: Size in bytes to unmap. 2473 * 2474 * The total amount of device-virtual memory unmapped is 2475 * @nr_pages * %PVR_DEVICE_PAGE_SIZE. 2476 * 2477 * Returns: 2478 * * 0 on success, or 2479 * * Any error code returned by pvr_page_table_ptr_init(), or 2480 * * Any error code returned by pvr_page_table_ptr_unmap(). 2481 */ 2482 int pvr_mmu_unmap(struct pvr_mmu_op_context *op_ctx, u64 device_addr, u64 size) 2483 { 2484 int err = pvr_mmu_op_context_set_curr_page(op_ctx, device_addr, false); 2485 2486 if (err) 2487 return err; 2488 2489 return pvr_mmu_op_context_unmap_curr_page(op_ctx, 2490 size >> PVR_DEVICE_PAGE_SHIFT); 2491 } 2492 2493 /** 2494 * pvr_mmu_map_sgl() - Map part of a scatter-gather table entry to 2495 * device-virtual memory. 2496 * @op_ctx: Target MMU op context pointing to the first page that should be 2497 * mapped. 2498 * @sgl: Target scatter-gather table entry. 2499 * @offset: Offset into @sgl to map from. Must result in a starting address 2500 * from @sgl which is CPU page-aligned. 2501 * @size: Size of the memory to be mapped in bytes. Must be a non-zero multiple 2502 * of the device page size. 2503 * @page_flags: Page options to be applied to every device-virtual memory page 2504 * in the created mapping. 2505 * 2506 * Return: 2507 * * 0 on success, 2508 * * -%EINVAL if the range specified by @offset and @size is not completely 2509 * within @sgl, or 2510 * * Any error encountered while creating a page with pvr_page_create(), or 2511 * * Any error encountered while advancing @op_ctx.curr_page with 2512 * pvr_mmu_op_context_next_page(). 2513 */ 2514 static int 2515 pvr_mmu_map_sgl(struct pvr_mmu_op_context *op_ctx, struct scatterlist *sgl, 2516 u64 offset, u64 size, struct pvr_page_flags_raw page_flags) 2517 { 2518 const unsigned int pages = size >> PVR_DEVICE_PAGE_SHIFT; 2519 dma_addr_t dma_addr = sg_dma_address(sgl) + offset; 2520 const unsigned int dma_len = sg_dma_len(sgl); 2521 struct pvr_page_table_ptr ptr_copy; 2522 unsigned int page; 2523 int err; 2524 2525 if (size > dma_len || offset > dma_len - size) 2526 return -EINVAL; 2527 2528 /* 2529 * Before progressing, save a copy of the start pointer so we can use 2530 * it again if we enter an error state and have to destroy pages. 2531 */ 2532 memcpy(&ptr_copy, &op_ctx->curr_page, sizeof(ptr_copy)); 2533 2534 /* 2535 * Create first page outside loop, as it doesn't require a page advance 2536 * beforehand. 2537 */ 2538 err = pvr_page_create(op_ctx, dma_addr, page_flags); 2539 if (err) 2540 return err; 2541 2542 for (page = 1; page < pages; ++page) { 2543 err = pvr_mmu_op_context_next_page(op_ctx, true); 2544 if (err) 2545 goto err_destroy_pages; 2546 2547 dma_addr += PVR_DEVICE_PAGE_SIZE; 2548 2549 err = pvr_page_create(op_ctx, dma_addr, page_flags); 2550 if (err) 2551 goto err_destroy_pages; 2552 } 2553 2554 return 0; 2555 2556 err_destroy_pages: 2557 memcpy(&op_ctx->curr_page, &ptr_copy, sizeof(op_ctx->curr_page)); 2558 if (pvr_mmu_op_context_unmap_curr_page(op_ctx, page)) 2559 drm_err(from_pvr_device(op_ctx->mmu_ctx->pvr_dev), 2560 "%s : Failure in unmapping pages\n", __func__); 2561 2562 return err; 2563 } 2564 2565 /** 2566 * pvr_mmu_map() - Map an object's virtual memory to physical memory. 2567 * @op_ctx: Target MMU op context. 2568 * @size: Size of memory to be mapped in bytes. Must be a non-zero multiple 2569 * of the device page size. 2570 * @flags: Flags from pvr_gem_object associated with the mapping. 2571 * @device_addr: Virtual device address to map to. Must be device page-aligned. 2572 * 2573 * Returns: 2574 * * 0 on success, or 2575 * * Any error code returned by pvr_page_table_ptr_init(), or 2576 * * Any error code returned by pvr_mmu_map_sgl(), or 2577 * * Any error code returned by pvr_page_table_ptr_next_page(). 2578 */ 2579 int pvr_mmu_map(struct pvr_mmu_op_context *op_ctx, u64 size, u64 flags, 2580 u64 device_addr) 2581 { 2582 struct pvr_page_table_ptr ptr_copy; 2583 struct pvr_page_flags_raw flags_raw; 2584 struct scatterlist *sgl; 2585 u64 mapped_size = 0; 2586 unsigned int count; 2587 int err; 2588 2589 if (!size) 2590 return 0; 2591 2592 if ((op_ctx->map.sgt_offset | size) & ~PVR_DEVICE_PAGE_MASK) 2593 return -EINVAL; 2594 2595 err = pvr_mmu_op_context_set_curr_page(op_ctx, device_addr, true); 2596 if (err) 2597 return -EINVAL; 2598 2599 memcpy(&ptr_copy, &op_ctx->curr_page, sizeof(ptr_copy)); 2600 2601 flags_raw = pvr_page_flags_raw_create(false, false, 2602 flags & DRM_PVR_BO_BYPASS_DEVICE_CACHE, 2603 flags & DRM_PVR_BO_PM_FW_PROTECT); 2604 2605 /* Map scatter gather table */ 2606 for_each_sgtable_dma_sg(op_ctx->map.sgt, sgl, count) { 2607 const size_t sgl_len = sg_dma_len(sgl); 2608 u64 sgl_offset, map_sgl_len; 2609 2610 if (sgl_len <= op_ctx->map.sgt_offset) { 2611 op_ctx->map.sgt_offset -= sgl_len; 2612 continue; 2613 } 2614 2615 sgl_offset = op_ctx->map.sgt_offset; 2616 map_sgl_len = min_t(u64, sgl_len - sgl_offset, size - mapped_size); 2617 2618 err = pvr_mmu_map_sgl(op_ctx, sgl, sgl_offset, map_sgl_len, 2619 flags_raw); 2620 if (err) 2621 break; 2622 2623 /* 2624 * Flag the L0 page table as requiring a flush when the MMU op 2625 * context is destroyed. 2626 */ 2627 pvr_mmu_op_context_require_sync(op_ctx, PVR_MMU_SYNC_LEVEL_0); 2628 2629 op_ctx->map.sgt_offset = 0; 2630 mapped_size += map_sgl_len; 2631 2632 if (mapped_size >= size) 2633 break; 2634 2635 err = pvr_mmu_op_context_next_page(op_ctx, true); 2636 if (err) 2637 break; 2638 } 2639 2640 if (err && mapped_size) { 2641 memcpy(&op_ctx->curr_page, &ptr_copy, sizeof(op_ctx->curr_page)); 2642 pvr_mmu_op_context_unmap_curr_page(op_ctx, 2643 mapped_size >> PVR_DEVICE_PAGE_SHIFT); 2644 } 2645 2646 return err; 2647 } 2648