1 /* $NetBSD: ttm_agp_backend.c,v 1.2 2018/08/27 04:58:37 riastradh Exp $ */ 2 3 /************************************************************************** 4 * 5 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA 6 * All Rights Reserved. 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a 9 * copy of this software and associated documentation files (the 10 * "Software"), to deal in the Software without restriction, including 11 * without limitation the rights to use, copy, modify, merge, publish, 12 * distribute, sub license, and/or sell copies of the Software, and to 13 * permit persons to whom the Software is furnished to do so, subject to 14 * the following conditions: 15 * 16 * The above copyright notice and this permission notice (including the 17 * next paragraph) shall be included in all copies or substantial portions 18 * of the Software. 19 * 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 23 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 24 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 25 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 26 * USE OR OTHER DEALINGS IN THE SOFTWARE. 27 * 28 **************************************************************************/ 29 /* 30 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> 31 * Keith Packard. 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: ttm_agp_backend.c,v 1.2 2018/08/27 04:58:37 riastradh Exp $"); 36 37 #define pr_fmt(fmt) "[TTM] " fmt 38 39 #include <drm/ttm/ttm_module.h> 40 #include <drm/ttm/ttm_bo_driver.h> 41 #include <drm/ttm/ttm_page_alloc.h> 42 #ifdef TTM_HAS_AGP 43 #include <drm/ttm/ttm_placement.h> 44 #include <linux/agp_backend.h> 45 #include <linux/module.h> 46 #include <linux/slab.h> 47 #include <linux/io.h> 48 #include <asm/agp.h> 49 50 struct ttm_agp_backend { 51 struct ttm_tt ttm; 52 struct agp_memory *mem; 53 struct agp_bridge_data *bridge; 54 }; 55 56 static int ttm_agp_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem) 57 { 58 struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm); 59 struct drm_mm_node *node = bo_mem->mm_node; 60 struct agp_memory *mem; 61 int ret, cached = (bo_mem->placement & TTM_PL_FLAG_CACHED); 62 unsigned i; 63 64 mem = agp_allocate_memory(agp_be->bridge, ttm->num_pages, AGP_USER_MEMORY); 65 if (unlikely(mem == NULL)) 66 return -ENOMEM; 67 68 mem->page_count = 0; 69 for (i = 0; i < ttm->num_pages; i++) { 70 struct page *page = ttm->pages[i]; 71 72 if (!page) 73 page = ttm->dummy_read_page; 74 75 mem->pages[mem->page_count++] = page; 76 } 77 agp_be->mem = mem; 78 79 mem->is_flushed = 1; 80 mem->type = (cached) ? AGP_USER_CACHED_MEMORY : AGP_USER_MEMORY; 81 82 ret = agp_bind_memory(mem, node->start); 83 if (ret) 84 pr_err("AGP Bind memory failed\n"); 85 86 return ret; 87 } 88 89 static int ttm_agp_unbind(struct ttm_tt *ttm) 90 { 91 struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm); 92 93 if (agp_be->mem) { 94 if (agp_be->mem->is_bound) 95 return agp_unbind_memory(agp_be->mem); 96 agp_free_memory(agp_be->mem); 97 agp_be->mem = NULL; 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->mem) 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_bo_device *bdev, 119 struct agp_bridge_data *bridge, 120 unsigned long size, uint32_t page_flags, 121 struct page *dummy_read_page) 122 { 123 struct ttm_agp_backend *agp_be; 124 125 agp_be = kmalloc(sizeof(*agp_be), GFP_KERNEL); 126 if (!agp_be) 127 return NULL; 128 129 agp_be->mem = NULL; 130 agp_be->bridge = bridge; 131 agp_be->ttm.func = &ttm_agp_func; 132 133 if (ttm_tt_init(&agp_be->ttm, bdev, size, page_flags, dummy_read_page)) { 134 kfree(agp_be); 135 return NULL; 136 } 137 138 return &agp_be->ttm; 139 } 140 EXPORT_SYMBOL(ttm_agp_tt_create); 141 142 int ttm_agp_tt_populate(struct ttm_tt *ttm) 143 { 144 if (ttm->state != tt_unpopulated) 145 return 0; 146 147 return ttm_pool_populate(ttm); 148 } 149 EXPORT_SYMBOL(ttm_agp_tt_populate); 150 151 void ttm_agp_tt_unpopulate(struct ttm_tt *ttm) 152 { 153 ttm_pool_unpopulate(ttm); 154 } 155 EXPORT_SYMBOL(ttm_agp_tt_unpopulate); 156 157 #endif 158