amdgpufb.c revision 1.1.2.2 1 1.1.2.2 pgoyette /* $NetBSD: amdgpufb.c,v 1.1.2.2 2018/09/06 06:56:09 pgoyette Exp $ */
2 1.1.2.2 pgoyette
3 1.1.2.2 pgoyette /*-
4 1.1.2.2 pgoyette * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.1.2.2 pgoyette * All rights reserved.
6 1.1.2.2 pgoyette *
7 1.1.2.2 pgoyette * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.2 pgoyette * by Taylor R. Campbell.
9 1.1.2.2 pgoyette *
10 1.1.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
11 1.1.2.2 pgoyette * modification, are permitted provided that the following conditions
12 1.1.2.2 pgoyette * are met:
13 1.1.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
14 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
15 1.1.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
17 1.1.2.2 pgoyette * documentation and/or other materials provided with the distribution.
18 1.1.2.2 pgoyette *
19 1.1.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.2 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.2 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.2 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.2 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.2 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.2 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.2 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.2 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.2 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.2 pgoyette * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.2 pgoyette */
31 1.1.2.2 pgoyette
32 1.1.2.2 pgoyette #include <sys/cdefs.h>
33 1.1.2.2 pgoyette __KERNEL_RCSID(0, "$NetBSD: amdgpufb.c,v 1.1.2.2 2018/09/06 06:56:09 pgoyette Exp $");
34 1.1.2.2 pgoyette
35 1.1.2.2 pgoyette #include <sys/types.h>
36 1.1.2.2 pgoyette #include <sys/bus.h>
37 1.1.2.2 pgoyette #include <sys/device.h>
38 1.1.2.2 pgoyette #include <sys/errno.h>
39 1.1.2.2 pgoyette
40 1.1.2.2 pgoyette #include <drm/drmP.h>
41 1.1.2.2 pgoyette #include <drm/drmfb.h>
42 1.1.2.2 pgoyette #include <drm/drmfb_pci.h>
43 1.1.2.2 pgoyette
44 1.1.2.2 pgoyette #include "amdgpu.h"
45 1.1.2.2 pgoyette #include "amdgpu_task.h"
46 1.1.2.2 pgoyette #include "amdgpufb.h"
47 1.1.2.2 pgoyette
48 1.1.2.2 pgoyette static int amdgpufb_match(device_t, cfdata_t, void *);
49 1.1.2.2 pgoyette static void amdgpufb_attach(device_t, device_t, void *);
50 1.1.2.2 pgoyette static int amdgpufb_detach(device_t, int);
51 1.1.2.2 pgoyette
52 1.1.2.2 pgoyette static void amdgpufb_attach_task(struct amdgpu_task *);
53 1.1.2.2 pgoyette
54 1.1.2.2 pgoyette static bool amdgpufb_shutdown(device_t, int);
55 1.1.2.2 pgoyette static paddr_t amdgpufb_drmfb_mmapfb(struct drmfb_softc *, off_t, int);
56 1.1.2.2 pgoyette
57 1.1.2.2 pgoyette struct amdgpufb_softc {
58 1.1.2.2 pgoyette struct drmfb_softc sc_drmfb; /* XXX Must be first. */
59 1.1.2.2 pgoyette device_t sc_dev;
60 1.1.2.2 pgoyette struct amdgpufb_attach_args sc_afa;
61 1.1.2.2 pgoyette struct amdgpu_task sc_attach_task;
62 1.1.2.2 pgoyette bool sc_scheduled:1;
63 1.1.2.2 pgoyette bool sc_attached:1;
64 1.1.2.2 pgoyette };
65 1.1.2.2 pgoyette
66 1.1.2.2 pgoyette static const struct drmfb_params amdgpufb_drmfb_params = {
67 1.1.2.2 pgoyette .dp_mmapfb = amdgpufb_drmfb_mmapfb,
68 1.1.2.2 pgoyette .dp_mmap = drmfb_pci_mmap,
69 1.1.2.2 pgoyette .dp_ioctl = drmfb_pci_ioctl,
70 1.1.2.2 pgoyette .dp_is_vga_console = drmfb_pci_is_vga_console,
71 1.1.2.2 pgoyette };
72 1.1.2.2 pgoyette
73 1.1.2.2 pgoyette CFATTACH_DECL_NEW(amdgpufb, sizeof(struct amdgpufb_softc),
74 1.1.2.2 pgoyette amdgpufb_match, amdgpufb_attach, amdgpufb_detach, NULL);
75 1.1.2.2 pgoyette
76 1.1.2.2 pgoyette static int
77 1.1.2.2 pgoyette amdgpufb_match(device_t parent, cfdata_t match, void *aux)
78 1.1.2.2 pgoyette {
79 1.1.2.2 pgoyette
80 1.1.2.2 pgoyette return 1;
81 1.1.2.2 pgoyette }
82 1.1.2.2 pgoyette
83 1.1.2.2 pgoyette static void
84 1.1.2.2 pgoyette amdgpufb_attach(device_t parent, device_t self, void *aux)
85 1.1.2.2 pgoyette {
86 1.1.2.2 pgoyette struct amdgpufb_softc *const sc = device_private(self);
87 1.1.2.2 pgoyette const struct amdgpufb_attach_args *const afa = aux;
88 1.1.2.2 pgoyette int error;
89 1.1.2.2 pgoyette
90 1.1.2.2 pgoyette sc->sc_dev = self;
91 1.1.2.2 pgoyette sc->sc_afa = *afa;
92 1.1.2.2 pgoyette sc->sc_scheduled = false;
93 1.1.2.2 pgoyette sc->sc_attached = false;
94 1.1.2.2 pgoyette
95 1.1.2.2 pgoyette aprint_naive("\n");
96 1.1.2.2 pgoyette aprint_normal("\n");
97 1.1.2.2 pgoyette
98 1.1.2.2 pgoyette amdgpu_task_init(&sc->sc_attach_task, &amdgpufb_attach_task);
99 1.1.2.2 pgoyette error = amdgpu_task_schedule(parent, &sc->sc_attach_task);
100 1.1.2.2 pgoyette if (error) {
101 1.1.2.2 pgoyette aprint_error_dev(self, "failed to schedule mode set: %d\n",
102 1.1.2.2 pgoyette error);
103 1.1.2.2 pgoyette goto fail0;
104 1.1.2.2 pgoyette }
105 1.1.2.2 pgoyette sc->sc_scheduled = true;
106 1.1.2.2 pgoyette
107 1.1.2.2 pgoyette /* Success! */
108 1.1.2.2 pgoyette return;
109 1.1.2.2 pgoyette
110 1.1.2.2 pgoyette fail0: return;
111 1.1.2.2 pgoyette }
112 1.1.2.2 pgoyette
113 1.1.2.2 pgoyette static int
114 1.1.2.2 pgoyette amdgpufb_detach(device_t self, int flags)
115 1.1.2.2 pgoyette {
116 1.1.2.2 pgoyette struct amdgpufb_softc *const sc = device_private(self);
117 1.1.2.2 pgoyette int error;
118 1.1.2.2 pgoyette
119 1.1.2.2 pgoyette if (sc->sc_scheduled)
120 1.1.2.2 pgoyette return EBUSY;
121 1.1.2.2 pgoyette
122 1.1.2.2 pgoyette if (sc->sc_attached) {
123 1.1.2.2 pgoyette pmf_device_deregister(self);
124 1.1.2.2 pgoyette error = drmfb_detach(&sc->sc_drmfb, flags);
125 1.1.2.2 pgoyette if (error) {
126 1.1.2.2 pgoyette /* XXX Ugh. */
127 1.1.2.2 pgoyette (void)pmf_device_register1(self, NULL, NULL,
128 1.1.2.2 pgoyette &amdgpufb_shutdown);
129 1.1.2.2 pgoyette return error;
130 1.1.2.2 pgoyette }
131 1.1.2.2 pgoyette sc->sc_attached = false;
132 1.1.2.2 pgoyette }
133 1.1.2.2 pgoyette
134 1.1.2.2 pgoyette return 0;
135 1.1.2.2 pgoyette }
136 1.1.2.2 pgoyette
137 1.1.2.2 pgoyette static void
138 1.1.2.2 pgoyette amdgpufb_attach_task(struct amdgpu_task *task)
139 1.1.2.2 pgoyette {
140 1.1.2.2 pgoyette struct amdgpufb_softc *const sc = container_of(task,
141 1.1.2.2 pgoyette struct amdgpufb_softc, sc_attach_task);
142 1.1.2.2 pgoyette const struct amdgpufb_attach_args *const afa = &sc->sc_afa;
143 1.1.2.2 pgoyette const struct drmfb_attach_args da = {
144 1.1.2.2 pgoyette .da_dev = sc->sc_dev,
145 1.1.2.2 pgoyette .da_fb_helper = afa->afa_fb_helper,
146 1.1.2.2 pgoyette .da_fb_sizes = &afa->afa_fb_sizes,
147 1.1.2.2 pgoyette .da_fb_vaddr = __UNVOLATILE(afa->afa_fb_ptr),
148 1.1.2.2 pgoyette .da_fb_linebytes = afa->afa_fb_linebytes,
149 1.1.2.2 pgoyette .da_params = &amdgpufb_drmfb_params,
150 1.1.2.2 pgoyette };
151 1.1.2.2 pgoyette int error;
152 1.1.2.2 pgoyette
153 1.1.2.2 pgoyette error = drmfb_attach(&sc->sc_drmfb, &da);
154 1.1.2.2 pgoyette if (error) {
155 1.1.2.2 pgoyette aprint_error_dev(sc->sc_dev, "failed to attach drmfb: %d\n",
156 1.1.2.2 pgoyette error);
157 1.1.2.2 pgoyette return;
158 1.1.2.2 pgoyette }
159 1.1.2.2 pgoyette
160 1.1.2.2 pgoyette if (!pmf_device_register1(sc->sc_dev, NULL, NULL, &amdgpufb_shutdown))
161 1.1.2.2 pgoyette aprint_error_dev(sc->sc_dev,
162 1.1.2.2 pgoyette "failed to register shutdown handler\n");
163 1.1.2.2 pgoyette
164 1.1.2.2 pgoyette sc->sc_attached = true;
165 1.1.2.2 pgoyette }
166 1.1.2.2 pgoyette
167 1.1.2.2 pgoyette static bool
168 1.1.2.2 pgoyette amdgpufb_shutdown(device_t self, int flags)
169 1.1.2.2 pgoyette {
170 1.1.2.2 pgoyette struct amdgpufb_softc *const sc = device_private(self);
171 1.1.2.2 pgoyette
172 1.1.2.2 pgoyette return drmfb_shutdown(&sc->sc_drmfb, flags);
173 1.1.2.2 pgoyette }
174 1.1.2.2 pgoyette
175 1.1.2.2 pgoyette static paddr_t
176 1.1.2.2 pgoyette amdgpufb_drmfb_mmapfb(struct drmfb_softc *drmfb, off_t offset, int prot)
177 1.1.2.2 pgoyette {
178 1.1.2.2 pgoyette struct amdgpufb_softc *const sc = container_of(drmfb,
179 1.1.2.2 pgoyette struct amdgpufb_softc, sc_drmfb);
180 1.1.2.2 pgoyette struct drm_fb_helper *const helper = sc->sc_afa.afa_fb_helper;
181 1.1.2.2 pgoyette struct drm_framebuffer *const fb = helper->fb;
182 1.1.2.2 pgoyette struct amdgpu_framebuffer *const rfb = container_of(fb,
183 1.1.2.2 pgoyette struct amdgpu_framebuffer, base);
184 1.1.2.2 pgoyette struct drm_gem_object *const gobj = rfb->obj;
185 1.1.2.2 pgoyette struct amdgpu_bo *const rbo = gem_to_amdgpu_bo(gobj);
186 1.1.2.2 pgoyette const unsigned num_pages __diagused = rbo->tbo.num_pages;
187 1.1.2.2 pgoyette int flags = 0;
188 1.1.2.2 pgoyette
189 1.1.2.2 pgoyette KASSERT(0 <= offset);
190 1.1.2.2 pgoyette KASSERT(offset < ((uintmax_t)num_pages << PAGE_SHIFT));
191 1.1.2.2 pgoyette KASSERT(rbo->tbo.mem.bus.is_iomem);
192 1.1.2.2 pgoyette
193 1.1.2.2 pgoyette if (ISSET(rbo->tbo.mem.placement, TTM_PL_FLAG_WC))
194 1.1.2.2 pgoyette flags |= BUS_SPACE_MAP_PREFETCHABLE;
195 1.1.2.2 pgoyette
196 1.1.2.2 pgoyette return bus_space_mmap(rbo->tbo.bdev->memt, rbo->tbo.mem.bus.base,
197 1.1.2.2 pgoyette rbo->tbo.mem.bus.offset + offset, prot, flags);
198 1.1.2.2 pgoyette }
199