// SPDX-License-Identifier: GPL-2.0-only /* * rsrc_nonstatic.c -- Resource management routines for !SS_CAP_STATIC_MAP sockets * * The initial developer of the original code is David A. Hinds * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds * are Copyright (C) 1999 David A. Hinds. All Rights Reserved. * * (C) 1999 David A. Hinds
*/
dev_info(&s->dev, "cs: IO port probe %#x-%#x:", base, base+num-1);
/* First, what does a floating port look like? */
b = kzalloc(256, GFP_KERNEL); if (!b) {
pr_cont("\n");
dev_err(&s->dev, "do_io_probe: unable to kmalloc 256 bytes\n"); return;
} for (i = base, most = 0; i < base+num; i += 8) {
res = claim_region(s, i, 8, IORESOURCE_IO, "PCMCIA ioprobe"); if (!res) continue;
hole = inb(i); for (j = 1; j < 8; j++) if (inb(i+j) != hole) break;
free_region(res); if ((j == 8) && (++b[hole] > b[most]))
most = hole; if (b[most] == 127) break;
}
kfree(b);
bad = any = 0; for (i = base; i < base+num; i += 8) {
res = claim_region(s, i, 8, IORESOURCE_IO, "PCMCIA ioprobe"); if (!res) { if (!any)
pr_cont(" excluding"); if (!bad)
bad = any = i; continue;
} for (j = 0; j < 8; j++) if (inb(i+j) != most) break;
free_region(res); if (j < 8) { if (!any)
pr_cont(" excluding"); if (!bad)
bad = any = i;
} else { if (bad) {
sub_interval(&s_data->io_db, bad, i-bad);
pr_cont(" %#x-%#x", bad, i-1);
bad = 0;
}
}
} if (bad) { if ((num > 16) && (bad == base) && (i == base+num)) {
sub_interval(&s_data->io_db, bad, i-bad);
pr_cont(" nothing: probe failed.\n"); return;
} else {
sub_interval(&s_data->io_db, bad, i-bad);
pr_cont(" %#x-%#x", bad, i-1);
}
}
/* * readable() - iomem validation function for cards with a valid CIS
*/ staticint readable(struct pcmcia_socket *s, struct resource *res, unsignedint *count)
{ int ret = -EINVAL;
if (s->fake_cis) {
dev_dbg(&s->dev, "fake CIS is being used: can't validate mem\n"); return 0;
}
s->cis_mem.res = res;
s->cis_virt = ioremap(res->start, s->map_size); if (s->cis_virt) {
mutex_unlock(&s->ops_mutex); /* as we're only called from pcmcia.c, we're safe */ if (s->callback->validate)
ret = s->callback->validate(s, count); /* invalidate mapping */
mutex_lock(&s->ops_mutex);
iounmap(s->cis_virt);
s->cis_virt = NULL;
}
s->cis_mem.res = NULL; if ((ret) || (*count == 0)) return -EINVAL; return 0;
}
/* * checksum() - iomem validation function for simple memory cards
*/ staticint checksum(struct pcmcia_socket *s, struct resource *res, unsignedint *value)
{
pccard_mem_map map; int i, a = 0, b = -1, d; void __iomem *virt;
/* Don't bother checking every word... */ for (i = 0; i < s->map_size; i += 44) {
d = readl(virt+i);
a += d;
b &= d;
}
map.flags = 0;
s->ops->set_mem_map(s, &map);
iounmap(virt);
}
if (b == -1) return -EINVAL;
*value = a;
return 0;
}
/** * do_validate_mem() - low level validate a memory region for PCMCIA use * @s: PCMCIA socket to validate * @base: start address of resource to check * @size: size of resource to check * @validate: validation function to use * * do_validate_mem() splits up the memory region which is to be checked * into two parts. Both are passed to the @validate() function. If * @validate() returns non-zero, or the value parameter to @validate() * is zero, or the value parameter is different between both calls, * the check fails, and -EINVAL is returned. Else, 0 is returned.
*/ staticint do_validate_mem(struct pcmcia_socket *s, unsignedlong base, unsignedlong size, int (*validate)(struct pcmcia_socket *s, struct resource *res, unsignedint *value))
{ struct socket_data *s_data = s->resource_data; struct resource *res1, *res2; unsignedint info1 = 1, info2 = 1; int ret = -EINVAL;
if (validate && !s->fake_cis) { /* move it to the validated data set */
ret = add_interval(&s_data->mem_db_valid, base, size); if (ret) return ret;
sub_interval(&s_data->mem_db, base, size);
}
return 0;
}
/** * do_mem_probe() - validate a memory region for PCMCIA use * @s: PCMCIA socket to validate * @base: start address of resource to check * @num: size of resource to check * @validate: validation function to use * @fallback: validation function to use if validate fails * * do_mem_probe() checks a memory region for use by the PCMCIA subsystem. * To do so, the area is split up into sensible parts, and then passed * into the @validate() function. Only if @validate() and @fallback() fail, * the area is marked as unavailable for use by the PCMCIA subsystem. The * function returns the size of the usable memory area.
*/ staticint do_mem_probe(struct pcmcia_socket *s, u_long base, u_long num, int (*validate)(struct pcmcia_socket *s, struct resource *res, unsignedint *value), int (*fallback)(struct pcmcia_socket *s, struct resource *res, unsignedint *value))
{ struct socket_data *s_data = s->resource_data;
u_long i, j, bad, fail, step;
dev_info(&s->dev, "cs: memory probe 0x%06lx-0x%06lx:",
base, base+num-1);
bad = fail = 0;
step = (num < 0x20000) ? 0x2000 : ((num>>4) & ~0x1fff); /* don't allow too large steps */ if (step > 0x800000)
step = 0x800000; /* cis_readable wants to map 2x map_size */ if (step < 2 * s->map_size)
step = 2 * s->map_size; for (i = j = base; i < base+num; i = j + step) { if (!fail) { for (j = i; j < base+num; j += step) { if (!do_validate_mem(s, j, step, validate)) break;
}
fail = ((i == base) && (j == base+num));
} if ((fail) && (fallback)) { for (j = i; j < base+num; j += step) if (!do_validate_mem(s, j, step, fallback)) break;
} if (i != j) { if (!bad)
pr_cont(" excluding");
pr_cont(" %#05lx-%#05lx", i, j-1);
sub_interval(&s_data->mem_db, i, j-i);
bad += j-i;
}
}
pr_cont("%s\n", !bad ? " clean" : ""); return num - bad;
}
#ifdef CONFIG_PCMCIA_PROBE
/** * inv_probe() - top-to-bottom search for one usuable high memory area * @s: PCMCIA socket to validate * @m: resource_map to check
*/ static u_long inv_probe(struct resource_map *m, struct pcmcia_socket *s)
{ struct socket_data *s_data = s->resource_data;
u_long ok; if (m == &s_data->mem_db) return 0;
ok = inv_probe(m->next, s); if (ok) { if (m->base >= 0x100000)
sub_interval(&s_data->mem_db, m->base, m->num); return ok;
} if (m->base < 0x100000) return 0; return do_mem_probe(s, m->base, m->num, readable, checksum);
}
/** * validate_mem() - memory probe function * @s: PCMCIA socket to validate * @probe_mask: MEM_PROBE_LOW | MEM_PROBE_HIGH * * The memory probe. If the memory list includes a 64K-aligned block * below 1MB, we probe in 64K chunks, and as soon as we accumulate at * least mem_limit free space, we quit. Returns 0 on usuable ports.
*/ staticint validate_mem(struct pcmcia_socket *s, unsignedint probe_mask)
{ struct resource_map *m, mm; staticunsignedchar order[] = { 0xd0, 0xe0, 0xc0, 0xf0 }; unsignedlong b, i, ok = 0; struct socket_data *s_data = s->resource_data;
/* We do up to four passes through the list */ if (probe_mask & MEM_PROBE_HIGH) { if (inv_probe(s_data->mem_db.next, s) > 0) return 0; if (s_data->mem_db_valid.next != &s_data->mem_db_valid) return 0;
dev_notice(&s->dev, "cs: warning: no high memory space available!\n"); return -ENODEV;
}
for (m = s_data->mem_db.next; m != &s_data->mem_db; m = mm.next) {
mm = *m; /* Only probe < 1 MB */ if (mm.base >= 0x100000) continue; if ((mm.base | mm.num) & 0xffff) {
ok += do_mem_probe(s, mm.base, mm.num, readable,
checksum); continue;
} /* Special probe for 64K-aligned block */ for (i = 0; i < 4; i++) {
b = order[i] << 12; if ((b >= mm.base) && (b+0x10000 <= mm.base+mm.num)) { if (ok >= mem_limit)
sub_interval(&s_data->mem_db, b, 0x10000); else
ok += do_mem_probe(s, b, 0x10000,
readable, checksum);
}
}
}
for (m = s_data->mem_db.next; m != &s_data->mem_db; m = mm.next) {
mm = *m;
ok += do_mem_probe(s, mm.base, mm.num, readable, checksum);
} if (ok > 0) return 0; return -ENODEV;
}
#endif/* CONFIG_PCMCIA_PROBE */
/** * pcmcia_nonstatic_validate_mem() - try to validate iomem for PCMCIA use * @s: PCMCIA socket to validate * * This is tricky... when we set up CIS memory, we try to validate * the memory window space allocations. * * Locking note: Must be called with skt_mutex held!
*/ staticint pcmcia_nonstatic_validate_mem(struct pcmcia_socket *s)
{ struct socket_data *s_data = s->resource_data; unsignedint probe_mask = MEM_PROBE_LOW; int ret;
if (!probe_mem || !(s->state & SOCKET_PRESENT)) return 0;
if (s->features & SS_CAP_PAGE_REGS)
probe_mask = MEM_PROBE_HIGH;
ret = validate_mem(s, probe_mask);
if (s_data->mem_db_valid.next != &s_data->mem_db_valid) return 0;
for (m = data->map->next; m != data->map; m = m->next) { unsignedlong map_start = m->base; unsignedlong map_end = m->base + m->num - 1;
/* * If the lower resources are not available, try aligning * to this entry of the resource database to see if it'll * fit here.
*/ if (start < map_start)
start = pcmcia_common_align(data, map_start);
/* * If we're above the area which was passed in, there's * no point proceeding.
*/ if (start >= res->end) break;
if ((start + size - 1) <= map_end) break;
}
/* * If we failed to find something suitable, ensure we fail.
*/ if (m == data->map)
start = res->end;
return start;
}
/* * Adjust an existing IO region allocation, but making sure that we don't * encroach outside the resources which the user supplied.
*/ staticint __nonstatic_adjust_io_region(struct pcmcia_socket *s, unsignedlong r_start, unsignedlong r_end)
{ struct resource_map *m; struct socket_data *s_data = s->resource_data; int ret = -ENOMEM;
for (m = s_data->io_db.next; m != &s_data->io_db; m = m->next) { unsignedlong start = m->base; unsignedlong end = m->base + m->num - 1;
These find ranges of I/O ports or memory addresses that are not currently allocated by other devices.
The 'align' field should reflect the number of bits of address that need to be preserved from the initial value of *base. It should be a power of two, greater than or equal to 'num'. A value of 0 means that all bits of *base are significant. *base should also be strictly less than 'align'.
#ifdef CONFIG_PCI if (s->cb_dev) {
ret = pci_bus_alloc_resource(s->cb_dev->bus, res, num, 1,
min, 0, pcmcia_align, &data);
} else #endif
ret = allocate_resource(&ioport_resource, res, num, min, ~0UL,
1, pcmcia_align, &data);
if (ret != 0) {
kfree(res);
res = NULL;
} return res;
}
staticint nonstatic_find_io(struct pcmcia_socket *s, unsignedint attr, unsignedint *base, unsignedint num, unsignedint align, struct resource **parent)
{ int i, ret = 0;
/* Check for an already-allocated window that must conflict with * what was asked for. It is a hack because it does not catch all * potential conflicts, just the most obvious ones.
*/ for (i = 0; i < MAX_IO_WIN; i++) { if (!s->io[i].res) continue;
if (!*base) continue;
if ((s->io[i].res->start & (align-1)) == *base) return -EBUSY;
}
for (i = 0; i < MAX_IO_WIN; i++) { struct resource *res = s->io[i].res; unsignedinttry;
data.mask = align - 1;
data.offset = base & data.mask;
for (i = 0; i < 2; i++) {
data.map = &s_data->mem_db_valid; if (low) {
max = 0x100000UL;
min = base < max ? base : 0;
} else {
max = ~0UL;
min = 0x100000UL + base;
}
for (j = 0; j < 2; j++) { #ifdef CONFIG_PCI if (s->cb_dev) {
ret = pci_bus_alloc_resource(s->cb_dev->bus,
res, num, 1, min, 0,
pcmcia_align, &data);
} else #endif
{
ret = allocate_resource(&iomem_resource,
res, num, min, max, 1,
pcmcia_align, &data);
} if (ret == 0) break;
data.map = &s_data->mem_db;
} if (ret == 0 || low) break;
low = 1;
}
if (ret != 0) {
kfree(res);
res = NULL;
} return res;
}
staticint adjust_memory(struct pcmcia_socket *s, unsignedint action, unsignedlong start, unsignedlong end)
{ struct socket_data *data = s->resource_data; unsignedlong size = end - start + 1; int ret = 0;
if (end < start) return -EINVAL;
switch (action) { case ADD_MANAGED_RESOURCE:
ret = add_interval(&data->mem_db, start, size); if (!ret)
do_mem_probe(s, start, size, NULL, NULL); break; case REMOVE_MANAGED_RESOURCE:
ret = sub_interval(&data->mem_db, start, size); break; default:
ret = -EINVAL;
}
#ifdefined(CONFIG_X86) /* on x86, avoid anything < 0x100 for it is often used for
* legacy platform devices */ if (start < 0x100)
start = 0x100; #endif
size = end - start + 1;
if (end < start) return -EINVAL;
if (end > IO_SPACE_LIMIT) return -EINVAL;
switch (action) { case ADD_MANAGED_RESOURCE: if (add_interval(&data->io_db, start, size) != 0) {
ret = -EBUSY; break;
} #ifdef CONFIG_PCMCIA_PROBE if (probe_io)
do_io_probe(s, start, size); #endif break; case REMOVE_MANAGED_RESOURCE:
sub_interval(&data->io_db, start, size); break; default:
ret = -EINVAL; break;
}
return ret;
}
#ifdef CONFIG_PCI staticint nonstatic_autoadd_resources(struct pcmcia_socket *s)
{ struct resource *res; int i, done = 0;
if (!s->cb_dev || !s->cb_dev->bus) return -ENODEV;
#ifdefined(CONFIG_X86) /* If this is the root bus, the risk of hitting some strange * system devices is too high: If a driver isn't loaded, the * resources are not claimed; even if a driver is loaded, it * may not request all resources or even the wrong one. We * can neither trust the rest of the kernel nor ACPI/PNP and * CRS parsing to get it right. Therefore, use several * safeguards: * * - Do not auto-add resources if the CardBus bridge is on * the PCI root bus * * - Avoid any I/O ports < 0x100. * * - On PCI-PCI bridges, only use resources which are set up * exclusively for the secondary PCI bus: the risk of hitting * system devices is quite low, as they usually aren't * connected to the secondary PCI bus.
*/ if (s->cb_dev->bus->number == 0) return -EINVAL;
for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
res = s->cb_dev->bus->resource[i]; #else
pci_bus_for_each_resource(s->cb_dev->bus, res, i) { #endif if (!res) continue;
if (res->flags & IORESOURCE_IO) { /* safeguard against the root resource, where the * risk of hitting any other device would be too
* high */ if (res == &ioport_resource) continue;
if (res->flags & IORESOURCE_MEM) { /* safeguard against the root resource, where the * risk of hitting any other device would be too
* high */ if (res == &iomem_resource) continue;
/* if we got at least one of IO, and one of MEM, we can be glad and
* activate the PCMCIA subsystem */ if (done == (IORESOURCE_MEM | IORESOURCE_IO))
s->resource_setup_done = 1;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.