1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
|
/* $OpenBSD: drm_linux.c,v 1.13 2017/07/01 16:14:10 kettenis Exp $ */
/*
* Copyright (c) 2013 Jonathan Gray <jsg@openbsd.org>
* Copyright (c) 2015, 2016 Mark Kettenis <kettenis@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <dev/pci/drm/drmP.h>
#include <dev/pci/ppbreg.h>
void
flush_barrier(void *arg)
{
int *barrier = arg;
*barrier = 1;
wakeup(barrier);
}
void
flush_workqueue(struct workqueue_struct *wq)
{
struct sleep_state sls;
struct task task;
int barrier = 0;
if (cold)
return;
task_set(&task, flush_barrier, &barrier);
task_add((struct taskq *)wq, &task);
while (!barrier) {
sleep_setup(&sls, &barrier, PWAIT, "flwqbar");
sleep_finish(&sls, !barrier);
}
}
void
flush_work(struct work_struct *work)
{
struct sleep_state sls;
struct task task;
int barrier = 0;
if (cold)
return;
task_set(&task, flush_barrier, &barrier);
task_add(work->tq, &task);
while (!barrier) {
sleep_setup(&sls, &barrier, PWAIT, "flwkbar");
sleep_finish(&sls, !barrier);
}
}
void
flush_delayed_work(struct delayed_work *dwork)
{
struct sleep_state sls;
struct task task;
int barrier = 0;
if (cold)
return;
while (timeout_pending(&dwork->to))
tsleep(&barrier, PWAIT, "fldwto", 1);
task_set(&task, flush_barrier, &barrier);
task_add(dwork->tq, &task);
while (!barrier) {
sleep_setup(&sls, &barrier, PWAIT, "fldwbar");
sleep_finish(&sls, !barrier);
}
}
struct timespec
ns_to_timespec(const int64_t nsec)
{
struct timespec ts;
int32_t rem;
if (nsec == 0) {
ts.tv_sec = 0;
ts.tv_nsec = 0;
return (ts);
}
ts.tv_sec = nsec / NSEC_PER_SEC;
rem = nsec % NSEC_PER_SEC;
if (rem < 0) {
ts.tv_sec--;
rem += NSEC_PER_SEC;
}
ts.tv_nsec = rem;
return (ts);
}
int64_t
timeval_to_ns(const struct timeval *tv)
{
return ((int64_t)tv->tv_sec * NSEC_PER_SEC) +
tv->tv_usec * NSEC_PER_USEC;
}
struct timeval
ns_to_timeval(const int64_t nsec)
{
struct timeval tv;
int32_t rem;
if (nsec == 0) {
tv.tv_sec = 0;
tv.tv_usec = 0;
return (tv);
}
tv.tv_sec = nsec / NSEC_PER_SEC;
rem = nsec % NSEC_PER_SEC;
if (rem < 0) {
tv.tv_sec--;
rem += NSEC_PER_SEC;
}
tv.tv_usec = rem / 1000;
return (tv);
}
int64_t
timeval_to_us(const struct timeval *tv)
{
return ((int64_t)tv->tv_sec * 1000000) + tv->tv_usec;
}
extern char *hw_vendor, *hw_prod;
static bool
dmi_found(const struct dmi_system_id *dsi)
{
int i, slot;
for (i = 0; i < nitems(dsi->matches); i++) {
slot = dsi->matches[i].slot;
switch (slot) {
case DMI_NONE:
break;
case DMI_SYS_VENDOR:
case DMI_BOARD_VENDOR:
if (hw_vendor != NULL &&
!strcmp(hw_vendor, dsi->matches[i].substr))
break;
else
return false;
case DMI_PRODUCT_NAME:
case DMI_BOARD_NAME:
if (hw_prod != NULL &&
!strcmp(hw_prod, dsi->matches[i].substr))
break;
else
return false;
default:
return false;
}
}
return true;
}
int
dmi_check_system(const struct dmi_system_id *sysid)
{
const struct dmi_system_id *dsi;
int num = 0;
for (dsi = sysid; dsi->matches[0].slot != 0 ; dsi++) {
if (dmi_found(dsi)) {
num++;
if (dsi->callback && dsi->callback(dsi))
break;
}
}
return (num);
}
struct vm_page *
alloc_pages(unsigned int gfp_mask, unsigned int order)
{
int flags = (gfp_mask & M_NOWAIT) ? UVM_PLA_NOWAIT : UVM_PLA_WAITOK;
struct pglist mlist;
if (gfp_mask & M_CANFAIL)
flags |= UVM_PLA_FAILOK;
if (gfp_mask & M_ZERO)
flags |= UVM_PLA_ZERO;
TAILQ_INIT(&mlist);
if (uvm_pglistalloc(PAGE_SIZE << order, 0, -1, PAGE_SIZE, 0,
&mlist, 1, flags))
return NULL;
return TAILQ_FIRST(&mlist);
}
void
__free_pages(struct vm_page *page, unsigned int order)
{
struct pglist mlist;
int i;
TAILQ_INIT(&mlist);
for (i = 0; i < (1 << order); i++)
TAILQ_INSERT_TAIL(&mlist, &page[i], pageq);
uvm_pglistfree(&mlist);
}
void *
kmap(struct vm_page *pg)
{
vaddr_t va;
#if defined (__HAVE_PMAP_DIRECT)
va = pmap_map_direct(pg);
#else
va = uvm_km_valloc_wait(phys_map, PAGE_SIZE);
pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pg), PROT_READ | PROT_WRITE);
pmap_update(pmap_kernel());
#endif
return (void *)va;
}
void
kunmap(void *addr)
{
vaddr_t va = (vaddr_t)addr;
#if defined (__HAVE_PMAP_DIRECT)
pmap_unmap_direct(va);
#else
pmap_kremove(va, PAGE_SIZE);
pmap_update(pmap_kernel());
uvm_km_free_wakeup(phys_map, va, PAGE_SIZE);
#endif
}
void *
vmap(struct vm_page **pages, unsigned int npages, unsigned long flags,
pgprot_t prot)
{
vaddr_t va;
paddr_t pa;
int i;
va = uvm_km_valloc(kernel_map, PAGE_SIZE * npages);
if (va == 0)
return NULL;
for (i = 0; i < npages; i++) {
pa = VM_PAGE_TO_PHYS(pages[i]) | prot;
pmap_enter(pmap_kernel(), va + (i * PAGE_SIZE), pa,
PROT_READ | PROT_WRITE,
PROT_READ | PROT_WRITE | PMAP_WIRED);
pmap_update(pmap_kernel());
}
return (void *)va;
}
void
vunmap(void *addr, size_t size)
{
vaddr_t va = (vaddr_t)addr;
pmap_remove(pmap_kernel(), va, va + size);
pmap_update(pmap_kernel());
uvm_km_free(kernel_map, va, size);
}
void
print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
int rowsize, int groupsize, const void *buf, size_t len, bool ascii)
{
const uint8_t *cbuf = buf;
int i;
for (i = 0; i < len; i++) {
if ((i % rowsize) == 0)
printf("%s", prefix_str);
printf("%02x", cbuf[i]);
if ((i % rowsize) == (rowsize - 1))
printf("\n");
else
printf(" ");
}
}
void *
memchr_inv(const void *s, int c, size_t n)
{
if (n != 0) {
const unsigned char *p = s;
do {
if (*p++ != (unsigned char)c)
return ((void *)(p - 1));
}while (--n != 0);
}
return (NULL);
}
int
panic_cmp(struct rb_node *a, struct rb_node *b)
{
panic(__func__);
}
#undef RB_ROOT
#define RB_ROOT(head) (head)->rbh_root
RB_GENERATE(linux_root, rb_node, __entry, panic_cmp);
/*
* This is a fairly minimal implementation of the Linux "idr" API. It
* probably isn't very efficient, and defenitely isn't RCU safe. The
* pre-load buffer is global instead of per-cpu; we rely on the kernel
* lock to make this work. We do randomize our IDs in order to make
* them harder to guess.
*/
int idr_cmp(struct idr_entry *, struct idr_entry *);
SPLAY_PROTOTYPE(idr_tree, idr_entry, entry, idr_cmp);
struct pool idr_pool;
struct idr_entry *idr_entry_cache;
void
idr_init(struct idr *idr)
{
static int initialized;
if (!initialized) {
pool_init(&idr_pool, sizeof(struct idr_entry), 0, IPL_TTY, 0,
"idrpl", NULL);
initialized = 1;
}
SPLAY_INIT(&idr->tree);
}
void
idr_destroy(struct idr *idr)
{
struct idr_entry *id;
while ((id = SPLAY_MIN(idr_tree, &idr->tree))) {
SPLAY_REMOVE(idr_tree, &idr->tree, id);
pool_put(&idr_pool, id);
}
}
void
idr_preload(unsigned int gfp_mask)
{
int flags = (gfp_mask & GFP_NOWAIT) ? PR_NOWAIT : PR_WAITOK;
KERNEL_ASSERT_LOCKED();
if (idr_entry_cache == NULL)
idr_entry_cache = pool_get(&idr_pool, flags);
}
int
idr_alloc(struct idr *idr, void *ptr, int start, int end,
unsigned int gfp_mask)
{
int flags = (gfp_mask & GFP_NOWAIT) ? PR_NOWAIT : PR_WAITOK;
struct idr_entry *id;
int begin;
KERNEL_ASSERT_LOCKED();
if (idr_entry_cache) {
id = idr_entry_cache;
idr_entry_cache = NULL;
} else {
id = pool_get(&idr_pool, flags);
if (id == NULL)
return -ENOMEM;
}
if (end <= 0)
end = INT_MAX;
#ifdef notyet
id->id = begin = start + arc4random_uniform(end - start);
#else
id->id = begin = start;
#endif
while (SPLAY_INSERT(idr_tree, &idr->tree, id)) {
if (++id->id == end)
id->id = start;
if (id->id == begin) {
pool_put(&idr_pool, id);
return -ENOSPC;
}
}
id->ptr = ptr;
return id->id;
}
void *
idr_replace(struct idr *idr, void *ptr, int id)
{
struct idr_entry find, *res;
void *old;
find.id = id;
res = SPLAY_FIND(idr_tree, &idr->tree, &find);
if (res == NULL)
return ERR_PTR(-ENOENT);
old = res->ptr;
res->ptr = ptr;
return old;
}
void
idr_remove(struct idr *idr, int id)
{
struct idr_entry find, *res;
find.id = id;
res = SPLAY_FIND(idr_tree, &idr->tree, &find);
if (res) {
SPLAY_REMOVE(idr_tree, &idr->tree, res);
pool_put(&idr_pool, res);
}
}
void *
idr_find(struct idr *idr, int id)
{
struct idr_entry find, *res;
find.id = id;
res = SPLAY_FIND(idr_tree, &idr->tree, &find);
if (res == NULL)
return NULL;
return res->ptr;
}
void *
idr_get_next(struct idr *idr, int *id)
{
struct idr_entry *res;
res = idr_find(idr, *id);
if (res == NULL)
res = SPLAY_MIN(idr_tree, &idr->tree);
else
res = SPLAY_NEXT(idr_tree, &idr->tree, res);
if (res == NULL)
return NULL;
*id = res->id;
return res->ptr;
}
int
idr_for_each(struct idr *idr, int (*func)(int, void *, void *), void *data)
{
struct idr_entry *id;
int ret;
SPLAY_FOREACH(id, idr_tree, &idr->tree) {
ret = func(id->id, id->ptr, data);
if (ret)
return ret;
}
return 0;
}
int
idr_cmp(struct idr_entry *a, struct idr_entry *b)
{
return (a->id < b->id ? -1 : a->id > b->id);
}
SPLAY_GENERATE(idr_tree, idr_entry, entry, idr_cmp);
void
ida_init(struct ida *ida)
{
ida->counter = 0;
}
void
ida_destroy(struct ida *ida)
{
}
void
ida_remove(struct ida *ida, int id)
{
}
int
ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
int flags)
{
if (end <= 0)
end = INT_MAX;
if (start > ida->counter)
ida->counter = start;
if (ida->counter >= end)
return -ENOSPC;
return ida->counter++;
}
int
sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
{
table->sgl = mallocarray(nents, sizeof(struct scatterlist),
M_DRM, gfp_mask);
if (table->sgl == NULL)
return -ENOMEM;
table->nents = table->orig_nents = nents;
return 0;
}
void
sg_free_table(struct sg_table *table)
{
free(table->sgl, M_DRM,
table->orig_nents * sizeof(struct scatterlist));
}
size_t
sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
const void *buf, size_t buflen)
{
panic("%s", __func__);
}
int
i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
{
void *cmd = NULL;
int cmdlen = 0;
int err, ret = 0;
int op;
if (adap->algo)
return adap->algo->master_xfer(adap, msgs, num);
iic_acquire_bus(&adap->ic, 0);
while (num > 2) {
op = (msgs->flags & I2C_M_RD) ? I2C_OP_READ : I2C_OP_WRITE;
err = iic_exec(&adap->ic, op, msgs->addr, NULL, 0,
msgs->buf, msgs->len, 0);
if (err) {
ret = -err;
goto fail;
}
msgs++;
num--;
ret++;
}
if (num > 1) {
cmd = msgs->buf;
cmdlen = msgs->len;
msgs++;
num--;
ret++;
}
op = (msgs->flags & I2C_M_RD) ? I2C_OP_READ_WITH_STOP : I2C_OP_WRITE_WITH_STOP;
err = iic_exec(&adap->ic, op, msgs->addr, cmd, cmdlen, msgs->buf, msgs->len, 0);
if (err) {
ret = -err;
goto fail;
}
msgs++;
ret++;
fail:
iic_release_bus(&adap->ic, 0);
return ret;
}
#if defined(__amd64__) || defined(__i386__)
/*
* This is a minimal implementation of the Linux vga_get/vga_put
* interface. In all likelyhood, it will only work for inteldrm(4) as
* it assumes that if there is another active VGA device in the
* system, it is sitting behind a PCI bridge.
*/
extern int pci_enumerate_bus(struct pci_softc *,
int (*)(struct pci_attach_args *), struct pci_attach_args *);
pcitag_t vga_bridge_tag;
int vga_bridge_disabled;
int
vga_disable_bridge(struct pci_attach_args *pa)
{
pcireg_t bhlc, bc;
if (pa->pa_domain != 0)
return 0;
bhlc = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
if (PCI_HDRTYPE_TYPE(bhlc) != 1)
return 0;
bc = pci_conf_read(pa->pa_pc, pa->pa_tag, PPB_REG_BRIDGECONTROL);
if ((bc & PPB_BC_VGA_ENABLE) == 0)
return 0;
bc &= ~PPB_BC_VGA_ENABLE;
pci_conf_write(pa->pa_pc, pa->pa_tag, PPB_REG_BRIDGECONTROL, bc);
vga_bridge_tag = pa->pa_tag;
vga_bridge_disabled = 1;
return 1;
}
void
vga_get_uninterruptible(struct pci_dev *pdev, int rsrc)
{
KASSERT(pdev->pci->sc_bridgetag == NULL);
pci_enumerate_bus(pdev->pci, vga_disable_bridge, NULL);
}
void
vga_put(struct pci_dev *pdev, int rsrc)
{
pcireg_t bc;
if (!vga_bridge_disabled)
return;
bc = pci_conf_read(pdev->pc, vga_bridge_tag, PPB_REG_BRIDGECONTROL);
bc |= PPB_BC_VGA_ENABLE;
pci_conf_write(pdev->pc, vga_bridge_tag, PPB_REG_BRIDGECONTROL, bc);
vga_bridge_disabled = 0;
}
#endif
/*
* ACPI types and interfaces.
*/
#if defined(__amd64__) || defined(__i386__)
#include "acpi.h"
#endif
#if NACPI > 0
#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>
acpi_status
acpi_get_table_with_size(const char *sig, int instance,
struct acpi_table_header **hdr, acpi_size *size)
{
struct acpi_softc *sc = acpi_softc;
struct acpi_q *entry;
KASSERT(instance == 1);
SIMPLEQ_FOREACH(entry, &sc->sc_tables, q_next) {
if (memcmp(entry->q_table, sig, strlen(sig)) == 0) {
*hdr = entry->q_table;
*size = (*hdr)->length;
return 0;
}
}
return AE_NOT_FOUND;
}
#endif
|