Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "pci.hpp" |
| 18 | |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 19 | extern "C" |
| 20 | { |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 21 | #include <pciaccess.h> |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 22 | } // extern "C" |
| 23 | |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 24 | #include <linux/pci_regs.h> |
| 25 | |
Patrick Venture | 2414161 | 2019-05-03 17:59:18 -0700 | [diff] [blame] | 26 | #include <cstring> |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 27 | #include <optional> |
| 28 | #include <vector> |
| 29 | |
| 30 | namespace host_tool |
| 31 | { |
| 32 | |
| 33 | std::vector<PciDevice> |
| 34 | PciUtilImpl::getPciDevices(std::optional<PciFilter> filter) |
| 35 | { |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 36 | struct pci_id_match match = {PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY, |
| 37 | PCI_MATCH_ANY}; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 38 | std::vector<PciDevice> results; |
| 39 | |
| 40 | if (filter.has_value()) |
| 41 | { |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 42 | match.vendor_id = filter.value().vid; |
| 43 | match.device_id = filter.value().did; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 46 | auto it = pci_id_match_iterator_create(&match); |
| 47 | struct pci_device* dev; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 48 | |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 49 | while ((dev = pci_device_next(it))) |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 50 | { |
| 51 | PciDevice item; |
| 52 | |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 53 | pci_device_probe(dev); |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 54 | |
| 55 | item.bus = dev->bus; |
| 56 | item.dev = dev->dev; |
| 57 | item.func = dev->func; |
| 58 | item.vid = dev->vendor_id; |
| 59 | item.did = dev->device_id; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 60 | |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 61 | for (int i = 0; i < PCI_STD_NUM_BARS; i++) |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 62 | { |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 63 | item.bars[i] = dev->regions[i].base_addr; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 64 | } |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 65 | |
| 66 | results.push_back(item); |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 69 | pci_iterator_destroy(it); |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 70 | return results; |
| 71 | } |
| 72 | |
| 73 | } // namespace host_tool |