1 // SPDX-License-Identifier: GPL-2.0
3 * rsparser.c - parses and encodes pnpbios resource data streams
6 #include <linux/ctype.h>
8 #include <linux/string.h>
11 #include <linux/pci.h>
13 inline void pcibios_penalize_isa_irq(int irq, int active)
16 #endif /* CONFIG_PCI */
21 /* standard resource tags */
22 #define SMALL_TAG_PNPVERNO 0x01
23 #define SMALL_TAG_LOGDEVID 0x02
24 #define SMALL_TAG_COMPATDEVID 0x03
25 #define SMALL_TAG_IRQ 0x04
26 #define SMALL_TAG_DMA 0x05
27 #define SMALL_TAG_STARTDEP 0x06
28 #define SMALL_TAG_ENDDEP 0x07
29 #define SMALL_TAG_PORT 0x08
30 #define SMALL_TAG_FIXEDPORT 0x09
31 #define SMALL_TAG_VENDOR 0x0e
32 #define SMALL_TAG_END 0x0f
33 #define LARGE_TAG 0x80
34 #define LARGE_TAG_MEM 0x81
35 #define LARGE_TAG_ANSISTR 0x82
36 #define LARGE_TAG_UNICODESTR 0x83
37 #define LARGE_TAG_VENDOR 0x84
38 #define LARGE_TAG_MEM32 0x85
39 #define LARGE_TAG_FIXEDMEM32 0x86
42 * Resource Data Stream Format:
44 * Allocated Resources (required)
46 * Resource Configuration Options (optional)
48 * Compitable Device IDs (optional)
56 static void pnpbios_parse_allocated_ioresource(struct pnp_dev *dev,
60 int end = start + len - 1;
62 if (len <= 0 || end >= 0x10003)
63 flags |= IORESOURCE_DISABLED;
65 pnp_add_io_resource(dev, start, end, flags);
68 static void pnpbios_parse_allocated_memresource(struct pnp_dev *dev,
72 int end = start + len - 1;
75 flags |= IORESOURCE_DISABLED;
77 pnp_add_mem_resource(dev, start, end, flags);
80 static unsigned char *pnpbios_parse_allocated_resource_data(struct pnp_dev *dev,
84 unsigned int len, tag;
85 int io, size, mask, i, flags;
90 pnp_dbg(&dev->dev, "parse allocated resources\n");
92 pnp_init_resources(dev);
94 while ((char *)p < (char *)end) {
96 /* determine the type of tag */
97 if (p[0] & LARGE_TAG) { /* large tag */
98 len = (p[2] << 8) | p[1];
100 } else { /* small tag */
102 tag = ((p[0] >> 3) & 0x0f);
110 io = *(short *)&p[4];
111 size = *(short *)&p[10];
112 pnpbios_parse_allocated_memresource(dev, io, size);
115 case LARGE_TAG_ANSISTR:
116 /* ignore this for now */
119 case LARGE_TAG_VENDOR:
123 case LARGE_TAG_MEM32:
127 size = *(int *)&p[16];
128 pnpbios_parse_allocated_memresource(dev, io, size);
131 case LARGE_TAG_FIXEDMEM32:
135 size = *(int *)&p[8];
136 pnpbios_parse_allocated_memresource(dev, io, size);
140 if (len < 2 || len > 3)
144 mask = p[1] + p[2] * 256;
145 for (i = 0; i < 16; i++, mask = mask >> 1)
149 pcibios_penalize_isa_irq(io, 1);
151 flags = IORESOURCE_DISABLED;
152 pnp_add_irq_resource(dev, io, flags);
161 for (i = 0; i < 8; i++, mask = mask >> 1)
165 flags = IORESOURCE_DISABLED;
166 pnp_add_dma_resource(dev, io, flags);
172 io = p[2] + p[3] * 256;
174 pnpbios_parse_allocated_ioresource(dev, io, size);
177 case SMALL_TAG_VENDOR:
181 case SMALL_TAG_FIXEDPORT:
184 io = p[1] + p[2] * 256;
186 pnpbios_parse_allocated_ioresource(dev, io, size);
191 return (unsigned char *)p;
194 default: /* an unknown tag */
196 dev_err(&dev->dev, "unknown tag %#x length %d\n",
201 /* continue to the next tag */
202 if (p[0] & LARGE_TAG)
208 dev_err(&dev->dev, "no end tag in resource structure\n");
214 * Resource Configuration Options
217 static __init void pnpbios_parse_mem_option(struct pnp_dev *dev,
218 unsigned char *p, int size,
219 unsigned int option_flags)
221 resource_size_t min, max, align, len;
224 min = ((p[5] << 8) | p[4]) << 8;
225 max = ((p[7] << 8) | p[6]) << 8;
226 align = (p[9] << 8) | p[8];
227 len = ((p[11] << 8) | p[10]) << 8;
229 pnp_register_mem_resource(dev, option_flags, min, max, align, len,
233 static __init void pnpbios_parse_mem32_option(struct pnp_dev *dev,
234 unsigned char *p, int size,
235 unsigned int option_flags)
237 resource_size_t min, max, align, len;
240 min = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
241 max = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
242 align = (p[15] << 24) | (p[14] << 16) | (p[13] << 8) | p[12];
243 len = (p[19] << 24) | (p[18] << 16) | (p[17] << 8) | p[16];
245 pnp_register_mem_resource(dev, option_flags, min, max, align, len,
249 static __init void pnpbios_parse_fixed_mem32_option(struct pnp_dev *dev,
250 unsigned char *p, int size,
251 unsigned int option_flags)
253 resource_size_t base, len;
256 base = (p[7] << 24) | (p[6] << 16) | (p[5] << 8) | p[4];
257 len = (p[11] << 24) | (p[10] << 16) | (p[9] << 8) | p[8];
259 pnp_register_mem_resource(dev, option_flags, base, base, 0, len, flags);
262 static __init void pnpbios_parse_irq_option(struct pnp_dev *dev,
263 unsigned char *p, int size,
264 unsigned int option_flags)
268 unsigned char flags = IORESOURCE_IRQ_HIGHEDGE;
270 bits = (p[2] << 8) | p[1];
272 bitmap_zero(map.bits, PNP_IRQ_NR);
273 bitmap_copy(map.bits, &bits, 16);
278 pnp_register_irq_resource(dev, option_flags, &map, flags);
281 static __init void pnpbios_parse_dma_option(struct pnp_dev *dev,
282 unsigned char *p, int size,
283 unsigned int option_flags)
285 pnp_register_dma_resource(dev, option_flags, p[1], p[2]);
288 static __init void pnpbios_parse_port_option(struct pnp_dev *dev,
289 unsigned char *p, int size,
290 unsigned int option_flags)
292 resource_size_t min, max, align, len;
295 min = (p[3] << 8) | p[2];
296 max = (p[5] << 8) | p[4];
299 flags = p[1] ? IORESOURCE_IO_16BIT_ADDR : 0;
300 pnp_register_port_resource(dev, option_flags, min, max, align, len,
304 static __init void pnpbios_parse_fixed_port_option(struct pnp_dev *dev,
305 unsigned char *p, int size,
306 unsigned int option_flags)
308 resource_size_t base, len;
310 base = (p[2] << 8) | p[1];
312 pnp_register_port_resource(dev, option_flags, base, base, 0, len,
313 IORESOURCE_IO_FIXED);
316 static __init unsigned char *
317 pnpbios_parse_resource_option_data(unsigned char *p, unsigned char *end,
320 unsigned int len, tag;
322 unsigned int option_flags;
327 pnp_dbg(&dev->dev, "parse resource options\n");
329 while ((char *)p < (char *)end) {
331 /* determine the type of tag */
332 if (p[0] & LARGE_TAG) { /* large tag */
333 len = (p[2] << 8) | p[1];
335 } else { /* small tag */
337 tag = ((p[0] >> 3) & 0x0f);
345 pnpbios_parse_mem_option(dev, p, len, option_flags);
348 case LARGE_TAG_MEM32:
351 pnpbios_parse_mem32_option(dev, p, len, option_flags);
354 case LARGE_TAG_FIXEDMEM32:
357 pnpbios_parse_fixed_mem32_option(dev, p, len,
362 if (len < 2 || len > 3)
364 pnpbios_parse_irq_option(dev, p, len, option_flags);
370 pnpbios_parse_dma_option(dev, p, len, option_flags);
376 pnpbios_parse_port_option(dev, p, len, option_flags);
379 case SMALL_TAG_VENDOR:
383 case SMALL_TAG_FIXEDPORT:
386 pnpbios_parse_fixed_port_option(dev, p, len,
390 case SMALL_TAG_STARTDEP:
393 priority = PNP_RES_PRIORITY_ACCEPTABLE;
396 option_flags = pnp_new_dependent_set(dev, priority);
399 case SMALL_TAG_ENDDEP:
408 default: /* an unknown tag */
410 dev_err(&dev->dev, "unknown tag %#x length %d\n",
415 /* continue to the next tag */
416 if (p[0] & LARGE_TAG)
422 dev_err(&dev->dev, "no end tag in resource structure\n");
428 * Compatible Device IDs
431 static unsigned char *pnpbios_parse_compatible_ids(unsigned char *p,
438 struct pnp_id *dev_id;
443 while ((char *)p < (char *)end) {
445 /* determine the type of tag */
446 if (p[0] & LARGE_TAG) { /* large tag */
447 len = (p[2] << 8) | p[1];
449 } else { /* small tag */
451 tag = ((p[0] >> 3) & 0x0f);
456 case LARGE_TAG_ANSISTR:
457 strncpy(dev->name, p + 3,
458 len >= PNP_NAME_LEN ? PNP_NAME_LEN - 2 : len);
460 PNP_NAME_LEN ? PNP_NAME_LEN - 1 : len] = '\0';
463 case SMALL_TAG_COMPATDEVID: /* compatible ID */
466 eisa_id = p[1] | p[2] << 8 | p[3] << 16 | p[4] << 24;
467 pnp_eisa_id_to_string(eisa_id & PNP_EISA_ID_MASK, id);
468 dev_id = pnp_add_id(dev, id);
475 return (unsigned char *)p;
478 default: /* an unknown tag */
480 dev_err(&dev->dev, "unknown tag %#x length %d\n",
485 /* continue to the next tag */
486 if (p[0] & LARGE_TAG)
492 dev_err(&dev->dev, "no end tag in resource structure\n");
498 * Allocated Resource Encoding
501 static void pnpbios_encode_mem(struct pnp_dev *dev, unsigned char *p,
502 struct resource *res)
507 if (pnp_resource_enabled(res)) {
509 len = resource_size(res);
515 p[4] = (base >> 8) & 0xff;
516 p[5] = ((base >> 8) >> 8) & 0xff;
517 p[6] = (base >> 8) & 0xff;
518 p[7] = ((base >> 8) >> 8) & 0xff;
519 p[10] = (len >> 8) & 0xff;
520 p[11] = ((len >> 8) >> 8) & 0xff;
522 pnp_dbg(&dev->dev, " encode mem %#lx-%#lx\n", base, base + len - 1);
525 static void pnpbios_encode_mem32(struct pnp_dev *dev, unsigned char *p,
526 struct resource *res)
531 if (pnp_resource_enabled(res)) {
533 len = resource_size(res);
540 p[5] = (base >> 8) & 0xff;
541 p[6] = (base >> 16) & 0xff;
542 p[7] = (base >> 24) & 0xff;
544 p[9] = (base >> 8) & 0xff;
545 p[10] = (base >> 16) & 0xff;
546 p[11] = (base >> 24) & 0xff;
548 p[17] = (len >> 8) & 0xff;
549 p[18] = (len >> 16) & 0xff;
550 p[19] = (len >> 24) & 0xff;
552 pnp_dbg(&dev->dev, " encode mem32 %#lx-%#lx\n", base, base + len - 1);
555 static void pnpbios_encode_fixed_mem32(struct pnp_dev *dev, unsigned char *p,
556 struct resource *res)
561 if (pnp_resource_enabled(res)) {
563 len = resource_size(res);
570 p[5] = (base >> 8) & 0xff;
571 p[6] = (base >> 16) & 0xff;
572 p[7] = (base >> 24) & 0xff;
574 p[9] = (len >> 8) & 0xff;
575 p[10] = (len >> 16) & 0xff;
576 p[11] = (len >> 24) & 0xff;
578 pnp_dbg(&dev->dev, " encode fixed_mem32 %#lx-%#lx\n", base,
582 static void pnpbios_encode_irq(struct pnp_dev *dev, unsigned char *p,
583 struct resource *res)
587 if (pnp_resource_enabled(res))
588 map = 1 << res->start;
593 p[2] = (map >> 8) & 0xff;
595 pnp_dbg(&dev->dev, " encode irq mask %#lx\n", map);
598 static void pnpbios_encode_dma(struct pnp_dev *dev, unsigned char *p,
599 struct resource *res)
603 if (pnp_resource_enabled(res))
604 map = 1 << res->start;
610 pnp_dbg(&dev->dev, " encode dma mask %#lx\n", map);
613 static void pnpbios_encode_port(struct pnp_dev *dev, unsigned char *p,
614 struct resource *res)
619 if (pnp_resource_enabled(res)) {
621 len = resource_size(res);
628 p[3] = (base >> 8) & 0xff;
630 p[5] = (base >> 8) & 0xff;
633 pnp_dbg(&dev->dev, " encode io %#lx-%#lx\n", base, base + len - 1);
636 static void pnpbios_encode_fixed_port(struct pnp_dev *dev, unsigned char *p,
637 struct resource *res)
639 unsigned long base = res->start;
640 unsigned long len = resource_size(res);
642 if (pnp_resource_enabled(res)) {
644 len = resource_size(res);
651 p[2] = (base >> 8) & 0xff;
654 pnp_dbg(&dev->dev, " encode fixed_io %#lx-%#lx\n", base,
658 static unsigned char *pnpbios_encode_allocated_resource_data(struct pnp_dev
663 unsigned int len, tag;
664 int port = 0, irq = 0, dma = 0, mem = 0;
669 while ((char *)p < (char *)end) {
671 /* determine the type of tag */
672 if (p[0] & LARGE_TAG) { /* large tag */
673 len = (p[2] << 8) | p[1];
675 } else { /* small tag */
677 tag = ((p[0] >> 3) & 0x0f);
685 pnpbios_encode_mem(dev, p,
686 pnp_get_resource(dev, IORESOURCE_MEM, mem));
690 case LARGE_TAG_MEM32:
693 pnpbios_encode_mem32(dev, p,
694 pnp_get_resource(dev, IORESOURCE_MEM, mem));
698 case LARGE_TAG_FIXEDMEM32:
701 pnpbios_encode_fixed_mem32(dev, p,
702 pnp_get_resource(dev, IORESOURCE_MEM, mem));
707 if (len < 2 || len > 3)
709 pnpbios_encode_irq(dev, p,
710 pnp_get_resource(dev, IORESOURCE_IRQ, irq));
717 pnpbios_encode_dma(dev, p,
718 pnp_get_resource(dev, IORESOURCE_DMA, dma));
725 pnpbios_encode_port(dev, p,
726 pnp_get_resource(dev, IORESOURCE_IO, port));
730 case SMALL_TAG_VENDOR:
734 case SMALL_TAG_FIXEDPORT:
737 pnpbios_encode_fixed_port(dev, p,
738 pnp_get_resource(dev, IORESOURCE_IO, port));
744 return (unsigned char *)p;
747 default: /* an unknown tag */
749 dev_err(&dev->dev, "unknown tag %#x length %d\n",
754 /* continue to the next tag */
755 if (p[0] & LARGE_TAG)
761 dev_err(&dev->dev, "no end tag in resource structure\n");
767 * Core Parsing Functions
770 int __init pnpbios_parse_data_stream(struct pnp_dev *dev,
771 struct pnp_bios_node *node)
773 unsigned char *p = (char *)node->data;
774 unsigned char *end = (char *)(node->data + node->size);
776 p = pnpbios_parse_allocated_resource_data(dev, p, end);
779 p = pnpbios_parse_resource_option_data(p, end, dev);
782 p = pnpbios_parse_compatible_ids(p, end, dev);
788 int pnpbios_read_resources_from_node(struct pnp_dev *dev,
789 struct pnp_bios_node *node)
791 unsigned char *p = (char *)node->data;
792 unsigned char *end = (char *)(node->data + node->size);
794 p = pnpbios_parse_allocated_resource_data(dev, p, end);
800 int pnpbios_write_resources_to_node(struct pnp_dev *dev,
801 struct pnp_bios_node *node)
803 unsigned char *p = (char *)node->data;
804 unsigned char *end = (char *)(node->data + node->size);
806 p = pnpbios_encode_allocated_resource_data(dev, p, end);