1 /*
2 * Copyright 2012 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Ben Skeggs
23 */
24
25 #include <core/object.h>
26 #include <subdev/bar.h>
27
28 struct nouveau_barobj {
29 struct nouveau_object base;
30 struct nouveau_vma vma;
31 void __iomem *iomem;
32 };
33
34 static int
nouveau_barobj_ctor(struct nouveau_object * parent,struct nouveau_object * engine,struct nouveau_oclass * oclass,void * mem,u32 size,struct nouveau_object ** pobject)35 nouveau_barobj_ctor(struct nouveau_object *parent,
36 struct nouveau_object *engine,
37 struct nouveau_oclass *oclass, void *mem, u32 size,
38 struct nouveau_object **pobject)
39 {
40 struct nouveau_bar *bar = (void *)engine;
41 struct nouveau_barobj *barobj;
42 int ret;
43
44 ret = nouveau_object_create(parent, engine, oclass, 0, &barobj);
45 *pobject = nv_object(barobj);
46 if (ret)
47 return ret;
48
49 ret = bar->kmap(bar, mem, NV_MEM_ACCESS_RW, &barobj->vma);
50 if (ret)
51 return ret;
52
53 barobj->iomem = bar->iomem + (u32)barobj->vma.offset;
54 return 0;
55 }
56
57 static void
nouveau_barobj_dtor(struct nouveau_object * object)58 nouveau_barobj_dtor(struct nouveau_object *object)
59 {
60 struct nouveau_bar *bar = (void *)object->engine;
61 struct nouveau_barobj *barobj = (void *)object;
62 if (barobj->vma.node)
63 bar->unmap(bar, &barobj->vma);
64 nouveau_object_destroy(&barobj->base);
65 }
66
67 static u32
nouveau_barobj_rd32(struct nouveau_object * object,u64 addr)68 nouveau_barobj_rd32(struct nouveau_object *object, u64 addr)
69 {
70 struct nouveau_barobj *barobj = (void *)object;
71 return ioread32_native(barobj->iomem + addr);
72 }
73
74 static void
nouveau_barobj_wr32(struct nouveau_object * object,u64 addr,u32 data)75 nouveau_barobj_wr32(struct nouveau_object *object, u64 addr, u32 data)
76 {
77 struct nouveau_barobj *barobj = (void *)object;
78 iowrite32_native(data, barobj->iomem + addr);
79 }
80
81 static struct nouveau_oclass
82 nouveau_barobj_oclass = {
83 .ofuncs = &(struct nouveau_ofuncs) {
84 .ctor = nouveau_barobj_ctor,
85 .dtor = nouveau_barobj_dtor,
86 .init = nouveau_object_init,
87 .fini = nouveau_object_fini,
88 .rd32 = nouveau_barobj_rd32,
89 .wr32 = nouveau_barobj_wr32,
90 },
91 };
92
93 int
nouveau_bar_alloc(struct nouveau_bar * bar,struct nouveau_object * parent,struct nouveau_mem * mem,struct nouveau_object ** pobject)94 nouveau_bar_alloc(struct nouveau_bar *bar, struct nouveau_object *parent,
95 struct nouveau_mem *mem, struct nouveau_object **pobject)
96 {
97 struct nouveau_object *engine = nv_object(bar);
98 return nouveau_object_ctor(parent, engine, &nouveau_barobj_oclass,
99 mem, 0, pobject);
100 }
101
102 int
nouveau_bar_create_(struct nouveau_object * parent,struct nouveau_object * engine,struct nouveau_oclass * oclass,int length,void ** pobject)103 nouveau_bar_create_(struct nouveau_object *parent,
104 struct nouveau_object *engine,
105 struct nouveau_oclass *oclass, int length, void **pobject)
106 {
107 struct nouveau_device *device = nv_device(parent);
108 struct nouveau_bar *bar;
109 int ret;
110
111 ret = nouveau_subdev_create_(parent, engine, oclass, 0, "BARCTL",
112 "bar", length, pobject);
113 bar = *pobject;
114 if (ret)
115 return ret;
116
117 bar->iomem = ioremap(pci_resource_start(device->pdev, 3),
118 pci_resource_len(device->pdev, 3));
119 return 0;
120 }
121
122 void
nouveau_bar_destroy(struct nouveau_bar * bar)123 nouveau_bar_destroy(struct nouveau_bar *bar)
124 {
125 if (bar->iomem)
126 iounmap(bar->iomem);
127 nouveau_subdev_destroy(&bar->base);
128 }
129
130 void
_nouveau_bar_dtor(struct nouveau_object * object)131 _nouveau_bar_dtor(struct nouveau_object *object)
132 {
133 struct nouveau_bar *bar = (void *)object;
134 nouveau_bar_destroy(bar);
135 }
136