xref: /openbmc/linux/drivers/gpu/drm/udl/udl_main.c (revision dc6a81c3)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Red Hat
4  *
5  * based in parts on udlfb.c:
6  * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
7  * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
8  * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
9  */
10 
11 #include <drm/drm.h>
12 #include <drm/drm_print.h>
13 #include <drm/drm_probe_helper.h>
14 
15 #include "udl_drv.h"
16 
17 /* -BULK_SIZE as per usb-skeleton. Can we get full page and avoid overhead? */
18 #define BULK_SIZE 512
19 
20 #define NR_USB_REQUEST_CHANNEL 0x12
21 
22 #define MAX_TRANSFER (PAGE_SIZE*16 - BULK_SIZE)
23 #define WRITES_IN_FLIGHT (4)
24 #define MAX_VENDOR_DESCRIPTOR_SIZE 256
25 
26 #define GET_URB_TIMEOUT	HZ
27 #define FREE_URB_TIMEOUT (HZ*2)
28 
29 static int udl_parse_vendor_descriptor(struct drm_device *dev,
30 				       struct usb_device *usbdev)
31 {
32 	struct udl_device *udl = to_udl(dev);
33 	char *desc;
34 	char *buf;
35 	char *desc_end;
36 
37 	u8 total_len = 0;
38 
39 	buf = kzalloc(MAX_VENDOR_DESCRIPTOR_SIZE, GFP_KERNEL);
40 	if (!buf)
41 		return false;
42 	desc = buf;
43 
44 	total_len = usb_get_descriptor(usbdev, 0x5f, /* vendor specific */
45 				    0, desc, MAX_VENDOR_DESCRIPTOR_SIZE);
46 	if (total_len > 5) {
47 		DRM_INFO("vendor descriptor length:%x data:%11ph\n",
48 			total_len, desc);
49 
50 		if ((desc[0] != total_len) || /* descriptor length */
51 		    (desc[1] != 0x5f) ||   /* vendor descriptor type */
52 		    (desc[2] != 0x01) ||   /* version (2 bytes) */
53 		    (desc[3] != 0x00) ||
54 		    (desc[4] != total_len - 2)) /* length after type */
55 			goto unrecognized;
56 
57 		desc_end = desc + total_len;
58 		desc += 5; /* the fixed header we've already parsed */
59 
60 		while (desc < desc_end) {
61 			u8 length;
62 			u16 key;
63 
64 			key = le16_to_cpu(*((u16 *) desc));
65 			desc += sizeof(u16);
66 			length = *desc;
67 			desc++;
68 
69 			switch (key) {
70 			case 0x0200: { /* max_area */
71 				u32 max_area;
72 				max_area = le32_to_cpu(*((u32 *)desc));
73 				DRM_DEBUG("DL chip limited to %d pixel modes\n",
74 					max_area);
75 				udl->sku_pixel_limit = max_area;
76 				break;
77 			}
78 			default:
79 				break;
80 			}
81 			desc += length;
82 		}
83 	}
84 
85 	goto success;
86 
87 unrecognized:
88 	/* allow udlfb to load for now even if firmware unrecognized */
89 	DRM_ERROR("Unrecognized vendor firmware descriptor\n");
90 
91 success:
92 	kfree(buf);
93 	return true;
94 }
95 
96 /*
97  * Need to ensure a channel is selected before submitting URBs
98  */
99 static int udl_select_std_channel(struct udl_device *udl)
100 {
101 	int ret;
102 	static const u8 set_def_chn[] = {0x57, 0xCD, 0xDC, 0xA7,
103 					 0x1C, 0x88, 0x5E, 0x15,
104 					 0x60, 0xFE, 0xC6, 0x97,
105 					 0x16, 0x3D, 0x47, 0xF2};
106 	void *sendbuf;
107 
108 	sendbuf = kmemdup(set_def_chn, sizeof(set_def_chn), GFP_KERNEL);
109 	if (!sendbuf)
110 		return -ENOMEM;
111 
112 	ret = usb_control_msg(udl->udev,
113 			      usb_sndctrlpipe(udl->udev, 0),
114 			      NR_USB_REQUEST_CHANNEL,
115 			      (USB_DIR_OUT | USB_TYPE_VENDOR), 0, 0,
116 			      sendbuf, sizeof(set_def_chn),
117 			      USB_CTRL_SET_TIMEOUT);
118 	kfree(sendbuf);
119 	return ret < 0 ? ret : 0;
120 }
121 
122 static void udl_release_urb_work(struct work_struct *work)
123 {
124 	struct urb_node *unode = container_of(work, struct urb_node,
125 					      release_urb_work.work);
126 
127 	up(&unode->dev->urbs.limit_sem);
128 }
129 
130 void udl_urb_completion(struct urb *urb)
131 {
132 	struct urb_node *unode = urb->context;
133 	struct udl_device *udl = unode->dev;
134 	unsigned long flags;
135 
136 	/* sync/async unlink faults aren't errors */
137 	if (urb->status) {
138 		if (!(urb->status == -ENOENT ||
139 		    urb->status == -ECONNRESET ||
140 		    urb->status == -ESHUTDOWN)) {
141 			DRM_ERROR("%s - nonzero write bulk status received: %d\n",
142 				__func__, urb->status);
143 		}
144 	}
145 
146 	urb->transfer_buffer_length = udl->urbs.size; /* reset to actual */
147 
148 	spin_lock_irqsave(&udl->urbs.lock, flags);
149 	list_add_tail(&unode->entry, &udl->urbs.list);
150 	udl->urbs.available++;
151 	spin_unlock_irqrestore(&udl->urbs.lock, flags);
152 
153 #if 0
154 	/*
155 	 * When using fb_defio, we deadlock if up() is called
156 	 * while another is waiting. So queue to another process.
157 	 */
158 	if (fb_defio)
159 		schedule_delayed_work(&unode->release_urb_work, 0);
160 	else
161 #endif
162 		up(&udl->urbs.limit_sem);
163 }
164 
165 static void udl_free_urb_list(struct drm_device *dev)
166 {
167 	struct udl_device *udl = to_udl(dev);
168 	int count = udl->urbs.count;
169 	struct list_head *node;
170 	struct urb_node *unode;
171 	struct urb *urb;
172 
173 	DRM_DEBUG("Waiting for completes and freeing all render urbs\n");
174 
175 	/* keep waiting and freeing, until we've got 'em all */
176 	while (count--) {
177 		down(&udl->urbs.limit_sem);
178 
179 		spin_lock_irq(&udl->urbs.lock);
180 
181 		node = udl->urbs.list.next; /* have reserved one with sem */
182 		list_del_init(node);
183 
184 		spin_unlock_irq(&udl->urbs.lock);
185 
186 		unode = list_entry(node, struct urb_node, entry);
187 		urb = unode->urb;
188 
189 		/* Free each separately allocated piece */
190 		usb_free_coherent(urb->dev, udl->urbs.size,
191 				  urb->transfer_buffer, urb->transfer_dma);
192 		usb_free_urb(urb);
193 		kfree(node);
194 	}
195 	udl->urbs.count = 0;
196 }
197 
198 static int udl_alloc_urb_list(struct drm_device *dev, int count, size_t size)
199 {
200 	struct udl_device *udl = to_udl(dev);
201 	struct urb *urb;
202 	struct urb_node *unode;
203 	char *buf;
204 	size_t wanted_size = count * size;
205 
206 	spin_lock_init(&udl->urbs.lock);
207 
208 retry:
209 	udl->urbs.size = size;
210 	INIT_LIST_HEAD(&udl->urbs.list);
211 
212 	sema_init(&udl->urbs.limit_sem, 0);
213 	udl->urbs.count = 0;
214 	udl->urbs.available = 0;
215 
216 	while (udl->urbs.count * size < wanted_size) {
217 		unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
218 		if (!unode)
219 			break;
220 		unode->dev = udl;
221 
222 		INIT_DELAYED_WORK(&unode->release_urb_work,
223 			  udl_release_urb_work);
224 
225 		urb = usb_alloc_urb(0, GFP_KERNEL);
226 		if (!urb) {
227 			kfree(unode);
228 			break;
229 		}
230 		unode->urb = urb;
231 
232 		buf = usb_alloc_coherent(udl->udev, size, GFP_KERNEL,
233 					 &urb->transfer_dma);
234 		if (!buf) {
235 			kfree(unode);
236 			usb_free_urb(urb);
237 			if (size > PAGE_SIZE) {
238 				size /= 2;
239 				udl_free_urb_list(dev);
240 				goto retry;
241 			}
242 			break;
243 		}
244 
245 		/* urb->transfer_buffer_length set to actual before submit */
246 		usb_fill_bulk_urb(urb, udl->udev, usb_sndbulkpipe(udl->udev, 1),
247 			buf, size, udl_urb_completion, unode);
248 		urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
249 
250 		list_add_tail(&unode->entry, &udl->urbs.list);
251 
252 		up(&udl->urbs.limit_sem);
253 		udl->urbs.count++;
254 		udl->urbs.available++;
255 	}
256 
257 	DRM_DEBUG("allocated %d %d byte urbs\n", udl->urbs.count, (int) size);
258 
259 	return udl->urbs.count;
260 }
261 
262 struct urb *udl_get_urb(struct drm_device *dev)
263 {
264 	struct udl_device *udl = to_udl(dev);
265 	int ret = 0;
266 	struct list_head *entry;
267 	struct urb_node *unode;
268 	struct urb *urb = NULL;
269 
270 	/* Wait for an in-flight buffer to complete and get re-queued */
271 	ret = down_timeout(&udl->urbs.limit_sem, GET_URB_TIMEOUT);
272 	if (ret) {
273 		DRM_INFO("wait for urb interrupted: %x available: %d\n",
274 		       ret, udl->urbs.available);
275 		goto error;
276 	}
277 
278 	spin_lock_irq(&udl->urbs.lock);
279 
280 	BUG_ON(list_empty(&udl->urbs.list)); /* reserved one with limit_sem */
281 	entry = udl->urbs.list.next;
282 	list_del_init(entry);
283 	udl->urbs.available--;
284 
285 	spin_unlock_irq(&udl->urbs.lock);
286 
287 	unode = list_entry(entry, struct urb_node, entry);
288 	urb = unode->urb;
289 
290 error:
291 	return urb;
292 }
293 
294 int udl_submit_urb(struct drm_device *dev, struct urb *urb, size_t len)
295 {
296 	struct udl_device *udl = to_udl(dev);
297 	int ret;
298 
299 	BUG_ON(len > udl->urbs.size);
300 
301 	urb->transfer_buffer_length = len; /* set to actual payload len */
302 	ret = usb_submit_urb(urb, GFP_ATOMIC);
303 	if (ret) {
304 		udl_urb_completion(urb); /* because no one else will */
305 		DRM_ERROR("usb_submit_urb error %x\n", ret);
306 	}
307 	return ret;
308 }
309 
310 int udl_init(struct udl_device *udl)
311 {
312 	struct drm_device *dev = &udl->drm;
313 	int ret = -ENOMEM;
314 
315 	DRM_DEBUG("\n");
316 
317 	mutex_init(&udl->gem_lock);
318 
319 	if (!udl_parse_vendor_descriptor(dev, udl->udev)) {
320 		ret = -ENODEV;
321 		DRM_ERROR("firmware not recognized. Assume incompatible device\n");
322 		goto err;
323 	}
324 
325 	if (udl_select_std_channel(udl))
326 		DRM_ERROR("Selecting channel failed\n");
327 
328 	if (!udl_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
329 		DRM_ERROR("udl_alloc_urb_list failed\n");
330 		goto err;
331 	}
332 
333 	DRM_DEBUG("\n");
334 	ret = udl_modeset_init(dev);
335 	if (ret)
336 		goto err;
337 
338 	drm_kms_helper_poll_init(dev);
339 
340 	return 0;
341 
342 err:
343 	if (udl->urbs.count)
344 		udl_free_urb_list(dev);
345 	DRM_ERROR("%d\n", ret);
346 	return ret;
347 }
348 
349 int udl_drop_usb(struct drm_device *dev)
350 {
351 	udl_free_urb_list(dev);
352 	return 0;
353 }
354 
355 void udl_fini(struct drm_device *dev)
356 {
357 	struct udl_device *udl = to_udl(dev);
358 
359 	drm_kms_helper_poll_fini(dev);
360 
361 	if (udl->urbs.count)
362 		udl_free_urb_list(dev);
363 }
364