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 #include "priv.h"
25 
26 #include <subdev/fb.h>
27 #include <subdev/mmu.h>
28 
29 struct nvkm_barobj {
30 	struct nvkm_object base;
31 	struct nvkm_vma vma;
32 	void __iomem *iomem;
33 };
34 
35 static int
36 nvkm_barobj_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
37 		 struct nvkm_oclass *oclass, void *data, u32 size,
38 		 struct nvkm_object **pobject)
39 {
40 	struct nvkm_device *device = nv_device(parent);
41 	struct nvkm_bar *bar = nvkm_bar(device);
42 	struct nvkm_mem *mem = data;
43 	struct nvkm_barobj *barobj;
44 	int ret;
45 
46 	ret = nvkm_object_create(parent, engine, oclass, 0, &barobj);
47 	*pobject = nv_object(barobj);
48 	if (ret)
49 		return ret;
50 
51 	ret = bar->kmap(bar, mem, NV_MEM_ACCESS_RW, &barobj->vma);
52 	if (ret)
53 		return ret;
54 
55 	barobj->iomem = ioremap(nv_device_resource_start(device, 3) +
56 				(u32)barobj->vma.offset, mem->size << 12);
57 	if (!barobj->iomem) {
58 		nv_warn(bar, "PRAMIN ioremap failed\n");
59 		return -ENOMEM;
60 	}
61 
62 	return 0;
63 }
64 
65 static void
66 nvkm_barobj_dtor(struct nvkm_object *object)
67 {
68 	struct nvkm_bar *bar = nvkm_bar(object);
69 	struct nvkm_barobj *barobj = (void *)object;
70 	if (barobj->vma.node) {
71 		if (barobj->iomem)
72 			iounmap(barobj->iomem);
73 		bar->unmap(bar, &barobj->vma);
74 	}
75 	nvkm_object_destroy(&barobj->base);
76 }
77 
78 static u32
79 nvkm_barobj_rd32(struct nvkm_object *object, u64 addr)
80 {
81 	struct nvkm_barobj *barobj = (void *)object;
82 	return ioread32_native(barobj->iomem + addr);
83 }
84 
85 static void
86 nvkm_barobj_wr32(struct nvkm_object *object, u64 addr, u32 data)
87 {
88 	struct nvkm_barobj *barobj = (void *)object;
89 	iowrite32_native(data, barobj->iomem + addr);
90 }
91 
92 static struct nvkm_oclass
93 nvkm_barobj_oclass = {
94 	.ofuncs = &(struct nvkm_ofuncs) {
95 		.ctor = nvkm_barobj_ctor,
96 		.dtor = nvkm_barobj_dtor,
97 		.init = nvkm_object_init,
98 		.fini = nvkm_object_fini,
99 		.rd32 = nvkm_barobj_rd32,
100 		.wr32 = nvkm_barobj_wr32,
101 	},
102 };
103 
104 int
105 nvkm_bar_alloc(struct nvkm_bar *bar, struct nvkm_object *parent,
106 	       struct nvkm_mem *mem, struct nvkm_object **pobject)
107 {
108 	struct nvkm_object *gpuobj;
109 	int ret = nvkm_object_ctor(parent, &parent->engine->subdev.object,
110 				   &nvkm_barobj_oclass, mem, 0, &gpuobj);
111 	if (ret == 0)
112 		*pobject = gpuobj;
113 	return ret;
114 }
115 
116 int
117 nvkm_bar_create_(struct nvkm_object *parent, struct nvkm_object *engine,
118 		 struct nvkm_oclass *oclass, int length, void **pobject)
119 {
120 	return nvkm_subdev_create_(parent, engine, oclass, 0, "BARCTL",
121 				   "bar", length, pobject);
122 }
123 
124 void
125 nvkm_bar_destroy(struct nvkm_bar *bar)
126 {
127 	nvkm_subdev_destroy(&bar->subdev);
128 }
129 
130 void
131 _nvkm_bar_dtor(struct nvkm_object *object)
132 {
133 	struct nvkm_bar *bar = (void *)object;
134 	nvkm_bar_destroy(bar);
135 }
136