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/ttm/ttm_module.h> 36 #include <drm/ttm/ttm_bo_driver.h> 37 #include <drm/ttm/ttm_page_alloc.h> 38 #include <drm/ttm/ttm_placement.h> 39 #include <linux/agp_backend.h> 40 #include <linux/module.h> 41 #include <linux/slab.h> 42 #include <linux/io.h> 43 #include <asm/agp.h> 44 45 #include <drm/drm_agpsupport.h> 46 47 struct ttm_agp_backend { 48 struct ttm_tt ttm; 49 int bound; 50 bus_addr_t addr; 51 struct drm_agp_head *agp; 52 }; 53 54 static int ttm_agp_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem) 55 { 56 struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm); 57 struct vm_page *dummy_read_page = ttm_bo_glob.dummy_read_page; 58 struct drm_mm_node *node = bo_mem->mm_node; 59 struct agp_softc *sc = agp_be->agp->agpdev; 60 bus_addr_t addr; 61 // int cached = (bo_mem->placement & TTM_PL_FLAG_CACHED); 62 unsigned i; 63 64 addr = sc->sc_apaddr + (node->start << PAGE_SHIFT); 65 for (i = 0; i < ttm->num_pages; i++) { 66 struct vm_page *page = ttm->pages[i]; 67 68 if (!page) 69 page = dummy_read_page; 70 71 sc->sc_methods->bind_page(sc->sc_chipc, addr, VM_PAGE_TO_PHYS(page), 0); 72 addr += PAGE_SIZE; 73 } 74 agp_flush_cache(); 75 sc->sc_methods->flush_tlb(sc->sc_chipc); 76 agp_be->addr = sc->sc_apaddr + (node->start << PAGE_SHIFT); 77 agp_be->bound = 1; 78 79 return 0; 80 } 81 82 static int ttm_agp_unbind(struct ttm_tt *ttm) 83 { 84 struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm); 85 struct agp_softc *sc = agp_be->agp->agpdev; 86 bus_addr_t addr; 87 unsigned i; 88 89 if (agp_be->bound) { 90 addr = agp_be->addr; 91 for (i = 0; i < ttm->num_pages; i++) { 92 sc->sc_methods->unbind_page(sc->sc_chipc, addr); 93 addr += PAGE_SIZE; 94 } 95 agp_flush_cache(); 96 sc->sc_methods->flush_tlb(sc->sc_chipc); 97 agp_be->bound = 0; 98 } 99 return 0; 100 } 101 102 static void ttm_agp_destroy(struct ttm_tt *ttm) 103 { 104 struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm); 105 106 if (agp_be->bound) 107 ttm_agp_unbind(ttm); 108 ttm_tt_fini(ttm); 109 kfree(agp_be); 110 } 111 112 static struct ttm_backend_func ttm_agp_func = { 113 .bind = ttm_agp_bind, 114 .unbind = ttm_agp_unbind, 115 .destroy = ttm_agp_destroy, 116 }; 117 118 struct ttm_tt *ttm_agp_tt_create(struct ttm_buffer_object *bo, 119 struct drm_agp_head *agp, 120 uint32_t page_flags) 121 { 122 struct ttm_agp_backend *agp_be; 123 124 agp_be = kmalloc(sizeof(*agp_be), GFP_KERNEL); 125 if (!agp_be) 126 return NULL; 127 128 agp_be->bound = 0; 129 agp_be->agp = agp; 130 agp_be->ttm.func = &ttm_agp_func; 131 132 if (ttm_tt_init(&agp_be->ttm, bo, page_flags)) { 133 return NULL; 134 } 135 136 return &agp_be->ttm; 137 } 138 EXPORT_SYMBOL(ttm_agp_tt_create); 139 140 int ttm_agp_tt_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx) 141 { 142 if (ttm->state != tt_unpopulated) 143 return 0; 144 145 return ttm_pool_populate(ttm, ctx); 146 } 147 EXPORT_SYMBOL(ttm_agp_tt_populate); 148 149 void ttm_agp_tt_unpopulate(struct ttm_tt *ttm) 150 { 151 ttm_pool_unpopulate(ttm); 152 } 153 EXPORT_SYMBOL(ttm_agp_tt_unpopulate); 154