Home | History | Annotate | Line # | Download | only in radeon
radeon_gart.c revision 1.1.1.3
      1 /*	$NetBSD: radeon_gart.c,v 1.1.1.3 2021/12/18 20:15:48 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright 2008 Advanced Micro Devices, Inc.
      5  * Copyright 2008 Red Hat Inc.
      6  * Copyright 2009 Jerome Glisse.
      7  *
      8  * Permission is hereby granted, free of charge, to any person obtaining a
      9  * copy of this software and associated documentation files (the "Software"),
     10  * to deal in the Software without restriction, including without limitation
     11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     12  * and/or sell copies of the Software, and to permit persons to whom the
     13  * Software is furnished to do so, subject to the following conditions:
     14  *
     15  * The above copyright notice and this permission notice shall be included in
     16  * all copies or substantial portions of the Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     21  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     24  * OTHER DEALINGS IN THE SOFTWARE.
     25  *
     26  * Authors: Dave Airlie
     27  *          Alex Deucher
     28  *          Jerome Glisse
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: radeon_gart.c,v 1.1.1.3 2021/12/18 20:15:48 riastradh Exp $");
     33 
     34 #include <linux/pci.h>
     35 #include <linux/vmalloc.h>
     36 
     37 #include <drm/radeon_drm.h>
     38 #ifdef CONFIG_X86
     39 #include <asm/set_memory.h>
     40 #endif
     41 #include "radeon.h"
     42 
     43 /*
     44  * GART
     45  * The GART (Graphics Aperture Remapping Table) is an aperture
     46  * in the GPU's address space.  System pages can be mapped into
     47  * the aperture and look like contiguous pages from the GPU's
     48  * perspective.  A page table maps the pages in the aperture
     49  * to the actual backing pages in system memory.
     50  *
     51  * Radeon GPUs support both an internal GART, as described above,
     52  * and AGP.  AGP works similarly, but the GART table is configured
     53  * and maintained by the northbridge rather than the driver.
     54  * Radeon hw has a separate AGP aperture that is programmed to
     55  * point to the AGP aperture provided by the northbridge and the
     56  * requests are passed through to the northbridge aperture.
     57  * Both AGP and internal GART can be used at the same time, however
     58  * that is not currently supported by the driver.
     59  *
     60  * This file handles the common internal GART management.
     61  */
     62 
     63 /*
     64  * Common GART table functions.
     65  */
     66 /**
     67  * radeon_gart_table_ram_alloc - allocate system ram for gart page table
     68  *
     69  * @rdev: radeon_device pointer
     70  *
     71  * Allocate system memory for GART page table
     72  * (r1xx-r3xx, non-pcie r4xx, rs400).  These asics require the
     73  * gart table to be in system memory.
     74  * Returns 0 for success, -ENOMEM for failure.
     75  */
     76 int radeon_gart_table_ram_alloc(struct radeon_device *rdev)
     77 {
     78 	void *ptr;
     79 
     80 	ptr = pci_alloc_consistent(rdev->pdev, rdev->gart.table_size,
     81 				   &rdev->gart.table_addr);
     82 	if (ptr == NULL) {
     83 		return -ENOMEM;
     84 	}
     85 #ifdef CONFIG_X86
     86 	if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 ||
     87 	    rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) {
     88 		set_memory_uc((unsigned long)ptr,
     89 			      rdev->gart.table_size >> PAGE_SHIFT);
     90 	}
     91 #endif
     92 	rdev->gart.ptr = ptr;
     93 	memset((void *)rdev->gart.ptr, 0, rdev->gart.table_size);
     94 	return 0;
     95 }
     96 
     97 /**
     98  * radeon_gart_table_ram_free - free system ram for gart page table
     99  *
    100  * @rdev: radeon_device pointer
    101  *
    102  * Free system memory for GART page table
    103  * (r1xx-r3xx, non-pcie r4xx, rs400).  These asics require the
    104  * gart table to be in system memory.
    105  */
    106 void radeon_gart_table_ram_free(struct radeon_device *rdev)
    107 {
    108 	if (rdev->gart.ptr == NULL) {
    109 		return;
    110 	}
    111 #ifdef CONFIG_X86
    112 	if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 ||
    113 	    rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) {
    114 		set_memory_wb((unsigned long)rdev->gart.ptr,
    115 			      rdev->gart.table_size >> PAGE_SHIFT);
    116 	}
    117 #endif
    118 	pci_free_consistent(rdev->pdev, rdev->gart.table_size,
    119 			    (void *)rdev->gart.ptr,
    120 			    rdev->gart.table_addr);
    121 	rdev->gart.ptr = NULL;
    122 	rdev->gart.table_addr = 0;
    123 }
    124 
    125 /**
    126  * radeon_gart_table_vram_alloc - allocate vram for gart page table
    127  *
    128  * @rdev: radeon_device pointer
    129  *
    130  * Allocate video memory for GART page table
    131  * (pcie r4xx, r5xx+).  These asics require the
    132  * gart table to be in video memory.
    133  * Returns 0 for success, error for failure.
    134  */
    135 int radeon_gart_table_vram_alloc(struct radeon_device *rdev)
    136 {
    137 	int r;
    138 
    139 	if (rdev->gart.robj == NULL) {
    140 		r = radeon_bo_create(rdev, rdev->gart.table_size,
    141 				     PAGE_SIZE, true, RADEON_GEM_DOMAIN_VRAM,
    142 				     0, NULL, NULL, &rdev->gart.robj);
    143 		if (r) {
    144 			return r;
    145 		}
    146 	}
    147 	return 0;
    148 }
    149 
    150 /**
    151  * radeon_gart_table_vram_pin - pin gart page table in vram
    152  *
    153  * @rdev: radeon_device pointer
    154  *
    155  * Pin the GART page table in vram so it will not be moved
    156  * by the memory manager (pcie r4xx, r5xx+).  These asics require the
    157  * gart table to be in video memory.
    158  * Returns 0 for success, error for failure.
    159  */
    160 int radeon_gart_table_vram_pin(struct radeon_device *rdev)
    161 {
    162 	uint64_t gpu_addr;
    163 	int r;
    164 
    165 	r = radeon_bo_reserve(rdev->gart.robj, false);
    166 	if (unlikely(r != 0))
    167 		return r;
    168 	r = radeon_bo_pin(rdev->gart.robj,
    169 				RADEON_GEM_DOMAIN_VRAM, &gpu_addr);
    170 	if (r) {
    171 		radeon_bo_unreserve(rdev->gart.robj);
    172 		return r;
    173 	}
    174 	r = radeon_bo_kmap(rdev->gart.robj, &rdev->gart.ptr);
    175 	if (r)
    176 		radeon_bo_unpin(rdev->gart.robj);
    177 	radeon_bo_unreserve(rdev->gart.robj);
    178 	rdev->gart.table_addr = gpu_addr;
    179 
    180 	if (!r) {
    181 		int i;
    182 
    183 		/* We might have dropped some GART table updates while it wasn't
    184 		 * mapped, restore all entries
    185 		 */
    186 		for (i = 0; i < rdev->gart.num_gpu_pages; i++)
    187 			radeon_gart_set_page(rdev, i, rdev->gart.pages_entry[i]);
    188 		mb();
    189 		radeon_gart_tlb_flush(rdev);
    190 	}
    191 
    192 	return r;
    193 }
    194 
    195 /**
    196  * radeon_gart_table_vram_unpin - unpin gart page table in vram
    197  *
    198  * @rdev: radeon_device pointer
    199  *
    200  * Unpin the GART page table in vram (pcie r4xx, r5xx+).
    201  * These asics require the gart table to be in video memory.
    202  */
    203 void radeon_gart_table_vram_unpin(struct radeon_device *rdev)
    204 {
    205 	int r;
    206 
    207 	if (rdev->gart.robj == NULL) {
    208 		return;
    209 	}
    210 	r = radeon_bo_reserve(rdev->gart.robj, false);
    211 	if (likely(r == 0)) {
    212 		radeon_bo_kunmap(rdev->gart.robj);
    213 		radeon_bo_unpin(rdev->gart.robj);
    214 		radeon_bo_unreserve(rdev->gart.robj);
    215 		rdev->gart.ptr = NULL;
    216 	}
    217 }
    218 
    219 /**
    220  * radeon_gart_table_vram_free - free gart page table vram
    221  *
    222  * @rdev: radeon_device pointer
    223  *
    224  * Free the video memory used for the GART page table
    225  * (pcie r4xx, r5xx+).  These asics require the gart table to
    226  * be in video memory.
    227  */
    228 void radeon_gart_table_vram_free(struct radeon_device *rdev)
    229 {
    230 	if (rdev->gart.robj == NULL) {
    231 		return;
    232 	}
    233 	radeon_bo_unref(&rdev->gart.robj);
    234 }
    235 
    236 /*
    237  * Common gart functions.
    238  */
    239 /**
    240  * radeon_gart_unbind - unbind pages from the gart page table
    241  *
    242  * @rdev: radeon_device pointer
    243  * @offset: offset into the GPU's gart aperture
    244  * @pages: number of pages to unbind
    245  *
    246  * Unbinds the requested pages from the gart page table and
    247  * replaces them with the dummy page (all asics).
    248  */
    249 void radeon_gart_unbind(struct radeon_device *rdev, unsigned offset,
    250 			int pages)
    251 {
    252 	unsigned t;
    253 	unsigned p;
    254 	int i, j;
    255 
    256 	if (!rdev->gart.ready) {
    257 		WARN(1, "trying to unbind memory from uninitialized GART !\n");
    258 		return;
    259 	}
    260 	t = offset / RADEON_GPU_PAGE_SIZE;
    261 	p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
    262 	for (i = 0; i < pages; i++, p++) {
    263 		if (rdev->gart.pages[p]) {
    264 			rdev->gart.pages[p] = NULL;
    265 			for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
    266 				rdev->gart.pages_entry[t] = rdev->dummy_page.entry;
    267 				if (rdev->gart.ptr) {
    268 					radeon_gart_set_page(rdev, t,
    269 							     rdev->dummy_page.entry);
    270 				}
    271 			}
    272 		}
    273 	}
    274 	if (rdev->gart.ptr) {
    275 		mb();
    276 		radeon_gart_tlb_flush(rdev);
    277 	}
    278 }
    279 
    280 /**
    281  * radeon_gart_bind - bind pages into the gart page table
    282  *
    283  * @rdev: radeon_device pointer
    284  * @offset: offset into the GPU's gart aperture
    285  * @pages: number of pages to bind
    286  * @pagelist: pages to bind
    287  * @dma_addr: DMA addresses of pages
    288  * @flags: RADEON_GART_PAGE_* flags
    289  *
    290  * Binds the requested pages to the gart page table
    291  * (all asics).
    292  * Returns 0 for success, -EINVAL for failure.
    293  */
    294 int radeon_gart_bind(struct radeon_device *rdev, unsigned offset,
    295 		     int pages, struct page **pagelist, dma_addr_t *dma_addr,
    296 		     uint32_t flags)
    297 {
    298 	unsigned t;
    299 	unsigned p;
    300 	uint64_t page_base, page_entry;
    301 	int i, j;
    302 
    303 	if (!rdev->gart.ready) {
    304 		WARN(1, "trying to bind memory to uninitialized GART !\n");
    305 		return -EINVAL;
    306 	}
    307 	t = offset / RADEON_GPU_PAGE_SIZE;
    308 	p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
    309 
    310 	for (i = 0; i < pages; i++, p++) {
    311 		rdev->gart.pages[p] = pagelist[i];
    312 		page_base = dma_addr[i];
    313 		for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
    314 			page_entry = radeon_gart_get_page_entry(page_base, flags);
    315 			rdev->gart.pages_entry[t] = page_entry;
    316 			if (rdev->gart.ptr) {
    317 				radeon_gart_set_page(rdev, t, page_entry);
    318 			}
    319 			page_base += RADEON_GPU_PAGE_SIZE;
    320 		}
    321 	}
    322 	if (rdev->gart.ptr) {
    323 		mb();
    324 		radeon_gart_tlb_flush(rdev);
    325 	}
    326 	return 0;
    327 }
    328 
    329 /**
    330  * radeon_gart_init - init the driver info for managing the gart
    331  *
    332  * @rdev: radeon_device pointer
    333  *
    334  * Allocate the dummy page and init the gart driver info (all asics).
    335  * Returns 0 for success, error for failure.
    336  */
    337 int radeon_gart_init(struct radeon_device *rdev)
    338 {
    339 	int r, i;
    340 
    341 	if (rdev->gart.pages) {
    342 		return 0;
    343 	}
    344 	/* We need PAGE_SIZE >= RADEON_GPU_PAGE_SIZE */
    345 	if (PAGE_SIZE < RADEON_GPU_PAGE_SIZE) {
    346 		DRM_ERROR("Page size is smaller than GPU page size!\n");
    347 		return -EINVAL;
    348 	}
    349 	r = radeon_dummy_page_init(rdev);
    350 	if (r)
    351 		return r;
    352 	/* Compute table size */
    353 	rdev->gart.num_cpu_pages = rdev->mc.gtt_size / PAGE_SIZE;
    354 	rdev->gart.num_gpu_pages = rdev->mc.gtt_size / RADEON_GPU_PAGE_SIZE;
    355 	DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
    356 		 rdev->gart.num_cpu_pages, rdev->gart.num_gpu_pages);
    357 	/* Allocate pages table */
    358 	rdev->gart.pages = vzalloc(array_size(sizeof(void *),
    359 				   rdev->gart.num_cpu_pages));
    360 	if (rdev->gart.pages == NULL) {
    361 		radeon_gart_fini(rdev);
    362 		return -ENOMEM;
    363 	}
    364 	rdev->gart.pages_entry = vmalloc(array_size(sizeof(uint64_t),
    365 						    rdev->gart.num_gpu_pages));
    366 	if (rdev->gart.pages_entry == NULL) {
    367 		radeon_gart_fini(rdev);
    368 		return -ENOMEM;
    369 	}
    370 	/* set GART entry to point to the dummy page by default */
    371 	for (i = 0; i < rdev->gart.num_gpu_pages; i++)
    372 		rdev->gart.pages_entry[i] = rdev->dummy_page.entry;
    373 	return 0;
    374 }
    375 
    376 /**
    377  * radeon_gart_fini - tear down the driver info for managing the gart
    378  *
    379  * @rdev: radeon_device pointer
    380  *
    381  * Tear down the gart driver info and free the dummy page (all asics).
    382  */
    383 void radeon_gart_fini(struct radeon_device *rdev)
    384 {
    385 	if (rdev->gart.ready) {
    386 		/* unbind pages */
    387 		radeon_gart_unbind(rdev, 0, rdev->gart.num_cpu_pages);
    388 	}
    389 	rdev->gart.ready = false;
    390 	vfree(rdev->gart.pages);
    391 	vfree(rdev->gart.pages_entry);
    392 	rdev->gart.pages = NULL;
    393 	rdev->gart.pages_entry = NULL;
    394 
    395 	radeon_dummy_page_fini(rdev);
    396 }
    397