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