• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <subdev/fb.h>
26 #include <core/mm.h>
27 
28 #include "priv.h"
29 
30 struct nv50_instmem_priv {
31 	struct nouveau_instmem base;
32 	spinlock_t lock;
33 	u64 addr;
34 };
35 
36 struct nv50_instobj_priv {
37 	struct nouveau_instobj base;
38 	struct nouveau_mem *mem;
39 };
40 
41 /******************************************************************************
42  * instmem object implementation
43  *****************************************************************************/
44 
45 static u32
nv50_instobj_rd32(struct nouveau_object * object,u64 offset)46 nv50_instobj_rd32(struct nouveau_object *object, u64 offset)
47 {
48 	struct nv50_instmem_priv *priv = (void *)object->engine;
49 	struct nv50_instobj_priv *node = (void *)object;
50 	unsigned long flags;
51 	u64 base = (node->mem->offset + offset) & 0xffffff00000ULL;
52 	u64 addr = (node->mem->offset + offset) & 0x000000fffffULL;
53 	u32 data;
54 
55 	spin_lock_irqsave(&priv->lock, flags);
56 	if (unlikely(priv->addr != base)) {
57 		nv_wr32(priv, 0x001700, base >> 16);
58 		priv->addr = base;
59 	}
60 	data = nv_rd32(priv, 0x700000 + addr);
61 	spin_unlock_irqrestore(&priv->lock, flags);
62 	return data;
63 }
64 
65 static void
nv50_instobj_wr32(struct nouveau_object * object,u64 offset,u32 data)66 nv50_instobj_wr32(struct nouveau_object *object, u64 offset, u32 data)
67 {
68 	struct nv50_instmem_priv *priv = (void *)object->engine;
69 	struct nv50_instobj_priv *node = (void *)object;
70 	unsigned long flags;
71 	u64 base = (node->mem->offset + offset) & 0xffffff00000ULL;
72 	u64 addr = (node->mem->offset + offset) & 0x000000fffffULL;
73 
74 	spin_lock_irqsave(&priv->lock, flags);
75 	if (unlikely(priv->addr != base)) {
76 		nv_wr32(priv, 0x001700, base >> 16);
77 		priv->addr = base;
78 	}
79 	nv_wr32(priv, 0x700000 + addr, data);
80 	spin_unlock_irqrestore(&priv->lock, flags);
81 }
82 
83 static void
nv50_instobj_dtor(struct nouveau_object * object)84 nv50_instobj_dtor(struct nouveau_object *object)
85 {
86 	struct nv50_instobj_priv *node = (void *)object;
87 	struct nouveau_fb *pfb = nouveau_fb(object);
88 	pfb->ram->put(pfb, &node->mem);
89 	nouveau_instobj_destroy(&node->base);
90 }
91 
92 static int
nv50_instobj_ctor(struct nouveau_object * parent,struct nouveau_object * engine,struct nouveau_oclass * oclass,void * data,u32 size,struct nouveau_object ** pobject)93 nv50_instobj_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
94 		  struct nouveau_oclass *oclass, void *data, u32 size,
95 		  struct nouveau_object **pobject)
96 {
97 	struct nouveau_fb *pfb = nouveau_fb(parent);
98 	struct nouveau_instobj_args *args = data;
99 	struct nv50_instobj_priv *node;
100 	int ret;
101 
102 	args->size  = max((args->size  + 4095) & ~4095, (u32)4096);
103 	args->align = max((args->align + 4095) & ~4095, (u32)4096);
104 
105 	ret = nouveau_instobj_create(parent, engine, oclass, &node);
106 	*pobject = nv_object(node);
107 	if (ret)
108 		return ret;
109 
110 	ret = pfb->ram->get(pfb, args->size, args->align, 0, 0x800, &node->mem);
111 	if (ret)
112 		return ret;
113 
114 	node->base.addr = node->mem->offset;
115 	node->base.size = node->mem->size << 12;
116 	node->mem->page_shift = 12;
117 	return 0;
118 }
119 
120 static struct nouveau_instobj_impl
121 nv50_instobj_oclass = {
122 	.base.ofuncs = &(struct nouveau_ofuncs) {
123 		.ctor = nv50_instobj_ctor,
124 		.dtor = nv50_instobj_dtor,
125 		.init = _nouveau_instobj_init,
126 		.fini = _nouveau_instobj_fini,
127 		.rd32 = nv50_instobj_rd32,
128 		.wr32 = nv50_instobj_wr32,
129 	},
130 };
131 
132 /******************************************************************************
133  * instmem subdev implementation
134  *****************************************************************************/
135 
136 static int
nv50_instmem_fini(struct nouveau_object * object,bool suspend)137 nv50_instmem_fini(struct nouveau_object *object, bool suspend)
138 {
139 	struct nv50_instmem_priv *priv = (void *)object;
140 	priv->addr = ~0ULL;
141 	return nouveau_instmem_fini(&priv->base, suspend);
142 }
143 
144 static int
nv50_instmem_ctor(struct nouveau_object * parent,struct nouveau_object * engine,struct nouveau_oclass * oclass,void * data,u32 size,struct nouveau_object ** pobject)145 nv50_instmem_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
146 		  struct nouveau_oclass *oclass, void *data, u32 size,
147 		  struct nouveau_object **pobject)
148 {
149 	struct nv50_instmem_priv *priv;
150 	int ret;
151 
152 	ret = nouveau_instmem_create(parent, engine, oclass, &priv);
153 	*pobject = nv_object(priv);
154 	if (ret)
155 		return ret;
156 
157 	spin_lock_init(&priv->lock);
158 	return 0;
159 }
160 
161 struct nouveau_oclass *
162 nv50_instmem_oclass = &(struct nouveau_instmem_impl) {
163 	.base.handle = NV_SUBDEV(INSTMEM, 0x50),
164 	.base.ofuncs = &(struct nouveau_ofuncs) {
165 		.ctor = nv50_instmem_ctor,
166 		.dtor = _nouveau_instmem_dtor,
167 		.init = _nouveau_instmem_init,
168 		.fini = nv50_instmem_fini,
169 	},
170 	.instobj = &nv50_instobj_oclass.base,
171 }.base;
172