1/* $NetBSD: nouveau_subdev_instmem_nv50.c,v 1.1.1.1 2014/08/06 12:36:31 riastradh Exp $ */
2
3/*
4 * Copyright 2012 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: nouveau_subdev_instmem_nv50.c,v 1.1.1.1 2014/08/06 12:36:31 riastradh Exp $");
29
30#include <subdev/fb.h>
31#include <core/mm.h>
32
33#include "priv.h"
34
35struct nv50_instmem_priv {
36 struct nouveau_instmem base;
37 spinlock_t lock;
38 u64 addr;
39};
40
41struct nv50_instobj_priv {
42 struct nouveau_instobj base;
43 struct nouveau_mem *mem;
44};
45
46/******************************************************************************
47 * instmem object implementation
48 *****************************************************************************/
49
50static u32
51nv50_instobj_rd32(struct nouveau_object *object, u64 offset)
52{
53 struct nv50_instmem_priv *priv = (void *)object->engine;
54 struct nv50_instobj_priv *node = (void *)object;
55 unsigned long flags;
56 u64 base = (node->mem->offset + offset) & 0xffffff00000ULL;
57 u64 addr = (node->mem->offset + offset) & 0x000000fffffULL;
58 u32 data;
59
60 spin_lock_irqsave(&priv->lock, flags);
61 if (unlikely(priv->addr != base)) {
62 nv_wr32(priv, 0x001700, base >> 16);
63 priv->addr = base;
64 }
65 data = nv_rd32(priv, 0x700000 + addr);
66 spin_unlock_irqrestore(&priv->lock, flags);
67 return data;
68}
69
70static void
71nv50_instobj_wr32(struct nouveau_object *object, u64 offset, u32 data)
72{
73 struct nv50_instmem_priv *priv = (void *)object->engine;
74 struct nv50_instobj_priv *node = (void *)object;
75 unsigned long flags;
76 u64 base = (node->mem->offset + offset) & 0xffffff00000ULL;
77 u64 addr = (node->mem->offset + offset) & 0x000000fffffULL;
78
79 spin_lock_irqsave(&priv->lock, flags);
80 if (unlikely(priv->addr != base)) {
81 nv_wr32(priv, 0x001700, base >> 16);
82 priv->addr = base;
83 }
84 nv_wr32(priv, 0x700000 + addr, data);
85 spin_unlock_irqrestore(&priv->lock, flags);
86}
87
88static void
89nv50_instobj_dtor(struct nouveau_object *object)
90{
91 struct nv50_instobj_priv *node = (void *)object;
92 struct nouveau_fb *pfb = nouveau_fb(object);
93 pfb->ram->put(pfb, &node->mem);
94 nouveau_instobj_destroy(&node->base);
95}
96
97static int
98nv50_instobj_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
99 struct nouveau_oclass *oclass, void *data, u32 size,
100 struct nouveau_object **pobject)
101{
102 struct nouveau_fb *pfb = nouveau_fb(parent);
103 struct nouveau_instobj_args *args = data;
104 struct nv50_instobj_priv *node;
105 int ret;
106
107 args->size = max((args->size + 4095) & ~4095, (u32)4096);
108 args->align = max((args->align + 4095) & ~4095, (u32)4096);
109
110 ret = nouveau_instobj_create(parent, engine, oclass, &node);
111 *pobject = nv_object(node);
112 if (ret)
113 return ret;
114
115 ret = pfb->ram->get(pfb, args->size, args->align, 0, 0x800, &node->mem);
116 if (ret)
117 return ret;
118
119 node->base.addr = node->mem->offset;
120 node->base.size = node->mem->size << 12;
121 node->mem->page_shift = 12;
122 return 0;
123}
124
125static struct nouveau_instobj_impl
126nv50_instobj_oclass = {
127 .base.ofuncs = &(struct nouveau_ofuncs) {
128 .ctor = nv50_instobj_ctor,
129 .dtor = nv50_instobj_dtor,
130 .init = _nouveau_instobj_init,
131 .fini = _nouveau_instobj_fini,
132 .rd32 = nv50_instobj_rd32,
133 .wr32 = nv50_instobj_wr32,
134 },
135};
136
137/******************************************************************************
138 * instmem subdev implementation
139 *****************************************************************************/
140
141static int
142nv50_instmem_fini(struct nouveau_object *object, bool suspend)
143{
144 struct nv50_instmem_priv *priv = (void *)object;
145 priv->addr = ~0ULL;
146 return nouveau_instmem_fini(&priv->base, suspend);
147}
148
149static int
150nv50_instmem_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
151 struct nouveau_oclass *oclass, void *data, u32 size,
152 struct nouveau_object **pobject)
153{
154 struct nv50_instmem_priv *priv;
155 int ret;
156
157 ret = nouveau_instmem_create(parent, engine, oclass, &priv);
158 *pobject = nv_object(priv);
159 if (ret)
160 return ret;
161
162 spin_lock_init(&priv->lock);
163 return 0;
164}
165
166struct nouveau_oclass *
167nv50_instmem_oclass = &(struct nouveau_instmem_impl) {
168 .base.handle = NV_SUBDEV(INSTMEM, 0x50),
169 .base.ofuncs = &(struct nouveau_ofuncs) {
170 .ctor = nv50_instmem_ctor,
171 .dtor = _nouveau_instmem_dtor,
172 .init = _nouveau_instmem_init,
173 .fini = nv50_instmem_fini,
174 },
175 .instobj = &nv50_instobj_oclass.base,
176}.base;
177