xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/nouveau/dispnv50/nouveau_dispnv50_lut.c (revision 1b592f33a70528671836bfa092de3e80d93631f4)
1 /*	$NetBSD: nouveau_dispnv50_lut.c,v 1.3 2021/12/19 10:49:47 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2018 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 #include <sys/cdefs.h>
25 __KERNEL_RCSID(0, "$NetBSD: nouveau_dispnv50_lut.c,v 1.3 2021/12/19 10:49:47 riastradh Exp $");
26 
27 #include "lut.h"
28 #include "disp.h"
29 
30 #include <drm/drm_color_mgmt.h>
31 #include <drm/drm_mode.h>
32 #include <drm/drm_property.h>
33 
34 #include <nvif/class.h>
35 
36 #ifdef __NetBSD__
37 #define	__iomem		__lut_iomem
38 #define	readw(p)	atomic_load_relaxed((const __iomem uint16_t *)(p))
39 #define	writew(v,p)	atomic_store_relaxed((__iomem uint16_t *)(p), (v))
40 #endif
41 
42 u32
nv50_lut_load(struct nv50_lut * lut,int buffer,struct drm_property_blob * blob,void (* load)(struct drm_color_lut *,int,void __iomem *))43 nv50_lut_load(struct nv50_lut *lut, int buffer, struct drm_property_blob *blob,
44 	      void (*load)(struct drm_color_lut *, int, void __iomem *))
45 {
46 	struct drm_color_lut *in = blob ? blob->data : NULL;
47 	void __iomem *mem = lut->mem[buffer].object.map.ptr;
48 	const u32 addr = lut->mem[buffer].addr;
49 	int i;
50 
51 	if (!in) {
52 		in = kvmalloc_array(1024, sizeof(*in), GFP_KERNEL);
53 		if (!WARN_ON(!in)) {
54 			for (i = 0; i < 1024; i++) {
55 				in[i].red   =
56 				in[i].green =
57 				in[i].blue  = (i << 16) >> 10;
58 			}
59 			load(in, 1024, mem);
60 			kvfree(in);
61 		}
62 	} else {
63 		load(in, drm_color_lut_size(blob), mem);
64 	}
65 
66 	return addr;
67 }
68 
69 void
nv50_lut_fini(struct nv50_lut * lut)70 nv50_lut_fini(struct nv50_lut *lut)
71 {
72 	int i;
73 	for (i = 0; i < ARRAY_SIZE(lut->mem); i++)
74 		nvif_mem_fini(&lut->mem[i]);
75 }
76 
77 int
nv50_lut_init(struct nv50_disp * disp,struct nvif_mmu * mmu,struct nv50_lut * lut)78 nv50_lut_init(struct nv50_disp *disp, struct nvif_mmu *mmu,
79 	      struct nv50_lut *lut)
80 {
81 	const u32 size = disp->disp->object.oclass < GF110_DISP ? 257 : 1025;
82 	int i;
83 	for (i = 0; i < ARRAY_SIZE(lut->mem); i++) {
84 		int ret = nvif_mem_init_map(mmu, NVIF_MEM_VRAM, size * 8,
85 					    &lut->mem[i]);
86 		if (ret)
87 			return ret;
88 	}
89 	return 0;
90 }
91