1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */ 2 /************************************************************************** 3 * 4 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA 5 * All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sub license, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice (including the 16 * next paragraph) shall be included in all copies or substantial portions 17 * of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 25 * USE OR OTHER DEALINGS IN THE SOFTWARE. 26 * 27 **************************************************************************/ 28 /* 29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> 30 * Keith Packard. 31 */ 32 33 #define pr_fmt(fmt) "[TTM] " fmt 34 35 #include <drm/drmP.h> 36 #include <drm/ttm/ttm_module.h> 37 #include <drm/ttm/ttm_bo_driver.h> 38 #include <drm/ttm/ttm_page_alloc.h> 39 #include <drm/ttm/ttm_placement.h> 40 #include <linux/agp_backend.h> 41 #include <linux/module.h> 42 #include <linux/slab.h> 43 #include <linux/io.h> 44 #include <asm/agp.h> 45 46 struct ttm_agp_backend { 47 struct ttm_tt ttm; 48 int bound; 49 bus_addr_t addr; 50 struct drm_agp_head *agp; 51 }; 52 53 static int ttm_agp_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem) 54 { 55 struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm); 56 struct vm_page *dummy_read_page = ttm->bdev->glob->dummy_read_page; 57 struct drm_mm_node *node = bo_mem->mm_node; 58 struct agp_softc *sc = agp_be->agp->agpdev; 59 bus_addr_t addr; 60 // int cached = (bo_mem->placement & TTM_PL_FLAG_CACHED); 61 unsigned i; 62 63 addr = sc->sc_apaddr + (node->start << PAGE_SHIFT); 64 for (i = 0; i < ttm->num_pages; i++) { 65 struct vm_page *page = ttm->pages[i]; 66 67 if (!page) 68 page = dummy_read_page; 69 70 sc->sc_methods->bind_page(sc->sc_chipc, addr, VM_PAGE_TO_PHYS(page), 0); 71 addr += PAGE_SIZE; 72 } 73 agp_flush_cache(); 74 sc->sc_methods->flush_tlb(sc->sc_chipc); 75 agp_be->addr = sc->sc_apaddr + (node->start << PAGE_SHIFT); 76 agp_be->bound = 1; 77 78 return 0; 79 } 80 81 static int ttm_agp_unbind(struct ttm_tt *ttm) 82 { 83 struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm); 84 struct agp_softc *sc = agp_be->agp->agpdev; 85 bus_addr_t addr; 86 unsigned i; 87 88 if (agp_be->bound) { 89 addr = agp_be->addr; 90 for (i = 0; i < ttm->num_pages; i++) { 91 sc->sc_methods->unbind_page(sc->sc_chipc, addr); 92 addr += PAGE_SIZE; 93 } 94 agp_flush_cache(); 95 sc->sc_methods->flush_tlb(sc->sc_chipc); 96 agp_be->bound = 0; 97 } 98 return 0; 99 } 100 101 static void ttm_agp_destroy(struct ttm_tt *ttm) 102 { 103 struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm); 104 105 if (agp_be->bound) 106 ttm_agp_unbind(ttm); 107 ttm_tt_fini(ttm); 108 kfree(agp_be); 109 } 110 111 static struct ttm_backend_func ttm_agp_func = { 112 .bind = ttm_agp_bind, 113 .unbind = ttm_agp_unbind, 114 .destroy = ttm_agp_destroy, 115 }; 116 117 struct ttm_tt *ttm_agp_tt_create(struct ttm_buffer_object *bo, 118 struct drm_agp_head *agp, 119 uint32_t page_flags) 120 { 121 struct ttm_agp_backend *agp_be; 122 123 agp_be = kmalloc(sizeof(*agp_be), GFP_KERNEL); 124 if (!agp_be) 125 return NULL; 126 127 agp_be->bound = 0; 128 agp_be->agp = agp; 129 agp_be->ttm.func = &ttm_agp_func; 130 131 if (ttm_tt_init(&agp_be->ttm, bo, page_flags)) { 132 return NULL; 133 } 134 135 return &agp_be->ttm; 136 } 137 EXPORT_SYMBOL(ttm_agp_tt_create); 138 139 int ttm_agp_tt_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx) 140 { 141 if (ttm->state != tt_unpopulated) 142 return 0; 143 144 return ttm_pool_populate(ttm, ctx); 145 } 146 EXPORT_SYMBOL(ttm_agp_tt_populate); 147 148 void ttm_agp_tt_unpopulate(struct ttm_tt *ttm) 149 { 150 ttm_pool_unpopulate(ttm); 151 } 152 EXPORT_SYMBOL(ttm_agp_tt_unpopulate); 153