xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/bios/nouveau_nvkm_subdev_bios_shadowof.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: nouveau_nvkm_subdev_bios_shadowof.c,v 1.3 2021/12/18 23:45:38 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  */
25 #include <sys/cdefs.h>
26 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_bios_shadowof.c,v 1.3 2021/12/18 23:45:38 riastradh Exp $");
27 
28 #include "priv.h"
29 
30 #include <core/pci.h>
31 
32 #ifdef __NetBSD__
33 #  define	__iomem	__of_rom_iomem
34 #endif
35 
36 #if defined(__powerpc__)
37 struct priv {
38 	const void __iomem *data;
39 	int size;
40 };
41 
42 static u32
of_read(void * data,u32 offset,u32 length,struct nvkm_bios * bios)43 of_read(void *data, u32 offset, u32 length, struct nvkm_bios *bios)
44 {
45 	struct priv *priv = data;
46 	if (offset < priv->size) {
47 		length = min_t(u32, length, priv->size - offset);
48 		memcpy_fromio(bios->data + offset, priv->data + offset, length);
49 		return length;
50 	}
51 	return 0;
52 }
53 
54 static u32
of_size(void * data)55 of_size(void *data)
56 {
57 	struct priv *priv = data;
58 	return priv->size;
59 }
60 
61 static void *
of_init(struct nvkm_bios * bios,const char * name)62 of_init(struct nvkm_bios *bios, const char *name)
63 {
64 	struct nvkm_device *device = bios->subdev.device;
65 	struct pci_dev *pdev = device->func->pci(device)->pdev;
66 	struct device_node *dn;
67 	struct priv *priv;
68 	if (!(dn = pci_device_to_OF_node(pdev)))
69 		return ERR_PTR(-ENODEV);
70 	if (!(priv = kzalloc(sizeof(*priv), GFP_KERNEL)))
71 		return ERR_PTR(-ENOMEM);
72 	if ((priv->data = of_get_property(dn, "NVDA,BMP", &priv->size)))
73 		return priv;
74 	kfree(priv);
75 	return ERR_PTR(-EINVAL);
76 }
77 
78 const struct nvbios_source
79 nvbios_of = {
80 	.name = "OpenFirmware",
81 	.init = of_init,
82 	.fini = (void(*)(void *))kfree,
83 	.read = of_read,
84 	.size = of_size,
85 	.rw = false,
86 	.ignore_checksum = true,
87 	.no_pcir = true,
88 };
89 #else
90 const struct nvbios_source
91 nvbios_of = {
92 };
93 #endif
94