support for platform devices
[linux-2.6-block.git] / drivers / gpu / drm / nouveau / core / subdev / bar / base.c
CommitLineData
3863c9bc
BS
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>
52225551
BS
26
27#include <subdev/fb.h>
28#include <subdev/vm.h>
29
30#include "priv.h"
3863c9bc
BS
31
32struct nouveau_barobj {
33 struct nouveau_object base;
34 struct nouveau_vma vma;
35 void __iomem *iomem;
36};
37
38static int
39nouveau_barobj_ctor(struct nouveau_object *parent,
40 struct nouveau_object *engine,
41 struct nouveau_oclass *oclass, void *mem, u32 size,
42 struct nouveau_object **pobject)
43{
44 struct nouveau_bar *bar = (void *)engine;
45 struct nouveau_barobj *barobj;
46 int ret;
47
48 ret = nouveau_object_create(parent, engine, oclass, 0, &barobj);
49 *pobject = nv_object(barobj);
50 if (ret)
51 return ret;
52
53 ret = bar->kmap(bar, mem, NV_MEM_ACCESS_RW, &barobj->vma);
54 if (ret)
55 return ret;
56
57 barobj->iomem = bar->iomem + (u32)barobj->vma.offset;
58 return 0;
59}
60
61static void
62nouveau_barobj_dtor(struct nouveau_object *object)
63{
64 struct nouveau_bar *bar = (void *)object->engine;
65 struct nouveau_barobj *barobj = (void *)object;
66 if (barobj->vma.node)
67 bar->unmap(bar, &barobj->vma);
68 nouveau_object_destroy(&barobj->base);
69}
70
71static u32
0a32241d 72nouveau_barobj_rd32(struct nouveau_object *object, u64 addr)
3863c9bc
BS
73{
74 struct nouveau_barobj *barobj = (void *)object;
75 return ioread32_native(barobj->iomem + addr);
76}
77
78static void
0a32241d 79nouveau_barobj_wr32(struct nouveau_object *object, u64 addr, u32 data)
3863c9bc
BS
80{
81 struct nouveau_barobj *barobj = (void *)object;
82 iowrite32_native(data, barobj->iomem + addr);
83}
84
85static struct nouveau_oclass
86nouveau_barobj_oclass = {
87 .ofuncs = &(struct nouveau_ofuncs) {
88 .ctor = nouveau_barobj_ctor,
89 .dtor = nouveau_barobj_dtor,
90 .init = nouveau_object_init,
91 .fini = nouveau_object_fini,
92 .rd32 = nouveau_barobj_rd32,
93 .wr32 = nouveau_barobj_wr32,
94 },
95};
96
97int
98nouveau_bar_alloc(struct nouveau_bar *bar, struct nouveau_object *parent,
99 struct nouveau_mem *mem, struct nouveau_object **pobject)
100{
101 struct nouveau_object *engine = nv_object(bar);
102 return nouveau_object_ctor(parent, engine, &nouveau_barobj_oclass,
103 mem, 0, pobject);
104}
105
106int
107nouveau_bar_create_(struct nouveau_object *parent,
108 struct nouveau_object *engine,
109 struct nouveau_oclass *oclass, int length, void **pobject)
110{
111 struct nouveau_device *device = nv_device(parent);
112 struct nouveau_bar *bar;
113 int ret;
114
115 ret = nouveau_subdev_create_(parent, engine, oclass, 0, "BARCTL",
116 "bar", length, pobject);
117 bar = *pobject;
118 if (ret)
119 return ret;
120
420b9469
AC
121 bar->iomem = ioremap(nv_device_resource_start(device, 3),
122 nv_device_resource_len(device, 3));
3863c9bc
BS
123 return 0;
124}
125
126void
127nouveau_bar_destroy(struct nouveau_bar *bar)
128{
129 if (bar->iomem)
130 iounmap(bar->iomem);
131 nouveau_subdev_destroy(&bar->base);
132}
133
134void
135_nouveau_bar_dtor(struct nouveau_object *object)
136{
137 struct nouveau_bar *bar = (void *)object;
138 nouveau_bar_destroy(bar);
139}