blob: 14ebd3650aa9d665094626a05473e90e6cf88282 [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
2 *
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * 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 NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27/*
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 * Keith Packard.
30 */
31
32#include "ttm/ttm_module.h"
33#include "ttm/ttm_bo_driver.h"
34#ifdef TTM_HAS_AGP
35#include "ttm/ttm_placement.h"
36#include <linux/agp_backend.h>
37#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020039#include <linux/io.h>
40#include <asm/agp.h>
41
42struct ttm_agp_backend {
Jerome Glisse649bf3c2011-11-01 20:46:13 -040043 struct ttm_tt ttm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020044 struct agp_memory *mem;
45 struct agp_bridge_data *bridge;
46};
47
Jerome Glisse649bf3c2011-11-01 20:46:13 -040048static int ttm_agp_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020049{
Jerome Glisse649bf3c2011-11-01 20:46:13 -040050 struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
51 struct drm_mm_node *node = bo_mem->mm_node;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020052 struct agp_memory *mem;
Jerome Glisse649bf3c2011-11-01 20:46:13 -040053 int ret, cached = (bo_mem->placement & TTM_PL_FLAG_CACHED);
54 unsigned i;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020055
Jerome Glisse649bf3c2011-11-01 20:46:13 -040056 mem = agp_allocate_memory(agp_be->bridge, ttm->num_pages, AGP_USER_MEMORY);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020057 if (unlikely(mem == NULL))
58 return -ENOMEM;
59
60 mem->page_count = 0;
Jerome Glisse649bf3c2011-11-01 20:46:13 -040061 for (i = 0; i < ttm->num_pages; i++) {
62 struct page *page = ttm->pages[i];
63
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020064 if (!page)
Jerome Glisse649bf3c2011-11-01 20:46:13 -040065 page = ttm->dummy_read_page;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020066
Dave Airlie07613ba2009-06-12 14:11:41 +100067 mem->pages[mem->page_count++] = page;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020068 }
69 agp_be->mem = mem;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020070
71 mem->is_flushed = 1;
72 mem->type = (cached) ? AGP_USER_CACHED_MEMORY : AGP_USER_MEMORY;
73
Ben Skeggsd961db72010-08-05 10:48:18 +100074 ret = agp_bind_memory(mem, node->start);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020075 if (ret)
76 printk(KERN_ERR TTM_PFX "AGP Bind memory failed.\n");
77
78 return ret;
79}
80
Jerome Glisse649bf3c2011-11-01 20:46:13 -040081static int ttm_agp_unbind(struct ttm_tt *ttm)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020082{
Jerome Glisse649bf3c2011-11-01 20:46:13 -040083 struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020084
Jerome Glisse649bf3c2011-11-01 20:46:13 -040085 if (agp_be->mem) {
86 if (agp_be->mem->is_bound)
87 return agp_unbind_memory(agp_be->mem);
88 agp_free_memory(agp_be->mem);
89 agp_be->mem = NULL;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020090 }
Jerome Glisse649bf3c2011-11-01 20:46:13 -040091 return 0;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020092}
93
Jerome Glisse649bf3c2011-11-01 20:46:13 -040094static void ttm_agp_destroy(struct ttm_tt *ttm)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020095{
Jerome Glisse649bf3c2011-11-01 20:46:13 -040096 struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020097
98 if (agp_be->mem)
Jerome Glisse649bf3c2011-11-01 20:46:13 -040099 ttm_agp_unbind(ttm);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200100 kfree(agp_be);
101}
102
103static struct ttm_backend_func ttm_agp_func = {
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200104 .bind = ttm_agp_bind,
105 .unbind = ttm_agp_unbind,
106 .destroy = ttm_agp_destroy,
107};
108
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400109struct ttm_tt *ttm_agp_tt_create(struct ttm_bo_device *bdev,
110 struct agp_bridge_data *bridge,
111 unsigned long size, uint32_t page_flags,
112 struct page *dummy_read_page)
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200113{
114 struct ttm_agp_backend *agp_be;
115
116 agp_be = kmalloc(sizeof(*agp_be), GFP_KERNEL);
117 if (!agp_be)
118 return NULL;
119
120 agp_be->mem = NULL;
121 agp_be->bridge = bridge;
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400122 agp_be->ttm.func = &ttm_agp_func;
123
124 if (ttm_tt_init(&agp_be->ttm, bdev, size, page_flags, dummy_read_page)) {
125 return NULL;
126 }
127
128 return &agp_be->ttm;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200129}
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400130EXPORT_SYMBOL(ttm_agp_tt_create);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200131
132#endif