Kyösti Mälkki (kyosti.malkki@gmail.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/865
-gerrit
commit a3f80a91276a306e434ecaae3b77c0ddf432c440 Author: Kyösti Mälkki kyosti.malkki@gmail.com Date: Mon Apr 30 12:05:39 2012 +0300
Add SIMBA: simulator for simple buses
With simba library, I/O and memory accesses to defined regions can be handled in a simulated state machine on the host computer. There is no need to recompile Qemu, as simple state machines can be implemented within this librarys sources.
An Intel 82801 (ICH4) SMBus host controller state machine is implemented as a functional example. At the moment it only parses the transactions.
Change-Id: Ia19067aebc81450fc98ff57f01921b11fec4eecb Signed-off-by: Kyösti Mälkki kyosti.malkki@gmail.com --- SerialICE/patches/lua-5.1.4-shared.diff | 43 +++ SerialICE/util/simba/COPYING | 339 +++++++++++++++++++++ SerialICE/util/simba/Makefile | 35 +++ SerialICE/util/simba/README | 57 ++++ SerialICE/util/simba/config.h | 11 + SerialICE/util/simba/i2c-i801.h | 152 ++++++++++ SerialICE/util/simba/i82801_sim.c | 493 +++++++++++++++++++++++++++++++ SerialICE/util/simba/io_hooks.c | 167 +++++++++++ SerialICE/util/simba/io_hooks.h | 6 + SerialICE/util/simba/main.c | 157 ++++++++++ SerialICE/util/simba/simba.c | 119 ++++++++ SerialICE/util/simba/simba.h | 134 +++++++++ SerialICE/util/simba/simba_lua.c | 70 +++++ 13 files changed, 1783 insertions(+), 0 deletions(-)
diff --git a/SerialICE/patches/lua-5.1.4-shared.diff b/SerialICE/patches/lua-5.1.4-shared.diff new file mode 100644 index 0000000..9371edd --- /dev/null +++ b/SerialICE/patches/lua-5.1.4-shared.diff @@ -0,0 +1,43 @@ +diff -ur lua-5.1.4/Makefile lua-5.1.4-patched/Makefile +--- lua-5.1.4/Makefile 2008-08-12 03:40:48.000000000 +0300 ++++ lua-5.1.4-patched/Makefile 2012-04-05 12:49:40.868289537 +0300 +@@ -43,7 +43,7 @@ + # What to install. + TO_BIN= lua luac + TO_INC= lua.h luaconf.h lualib.h lauxlib.h ../etc/lua.hpp +-TO_LIB= liblua.a ++TO_LIB= liblua.a liblua.so + TO_MAN= lua.1 luac.1 + + # Lua version and release. +diff -ur lua-5.1.4/src/Makefile lua-5.1.4-patched/src/Makefile +--- lua-5.1.4/src/Makefile 2012-04-05 20:49:22.636290315 +0300 ++++ lua-5.1.4-patched/src/Makefile 2012-04-05 12:44:28.684293164 +0300 +@@ -23,6 +23,8 @@ + PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris + + LUA_A= liblua.a ++LUA_SO= liblua.so ++ + CORE_O= lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \ + lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \ + lundump.o lvm.o lzio.o +@@ -36,7 +38,7 @@ + LUAC_O= luac.o print.o + + ALL_O= $(CORE_O) $(LIB_O) $(LUA_O) $(LUAC_O) +-ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) ++ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) $(LUA_SO) + ALL_A= $(LUA_A) + + default: $(PLAT) +@@ -51,6 +53,9 @@ + $(AR) $@ $? + $(RANLIB) $@ + ++$(LUA_SO): $(CORE_O) $(LIB_O) ++ $(CC) -o $@ -shared $? ++ + $(LUA_T): $(LUA_O) $(LUA_A) + $(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(LUA_A) $(LIBS) + diff --git a/SerialICE/util/simba/COPYING b/SerialICE/util/simba/COPYING new file mode 100644 index 0000000..d511905 --- /dev/null +++ b/SerialICE/util/simba/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + <signature of Ty Coon>, 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/SerialICE/util/simba/Makefile b/SerialICE/util/simba/Makefile new file mode 100644 index 0000000..54e27ef --- /dev/null +++ b/SerialICE/util/simba/Makefile @@ -0,0 +1,35 @@ +## +## Copyright (C) 2012 Kyösti Mälkki kyosti.malkki@gmail.com +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; version 2 of the License. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, write to the Free Software +## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +## + +CFLAGS += +LDFLAGS += -L$(cwd) -ldl -llua -lm + +cwd = $(shell pwd) + +drivers := i82801_sim.c i2c-i801.h +simba_src := simba.c simba.h simba_lua.c $(drivers) + +default: dump_smbus libsimba.so + +dump_smbus : main.c io_hooks.c io_hooks.h $(simba_src) + gcc $(CFLAGS) $(LDFLAGS) -o $@ $^ + +libsimba.so : $(simba_src) + gcc $(CFLAGS) -fPIC -shared -o $@ $^ + +clean: + -rm dump_smbus libsimba.so diff --git a/SerialICE/util/simba/README b/SerialICE/util/simba/README new file mode 100644 index 0000000..bcaa0f9 --- /dev/null +++ b/SerialICE/util/simba/README @@ -0,0 +1,57 @@ + +Serialice SMBus Host Controller simulator + +Not much here yet, but might already mostly work with many +Intel 82801 southbridges (aka ICHxx). There is a list of +PCI IDs in the i2c-i801.c file which have some chance. + +The common byte/word/block SMBus reads and writes have gone +through some testing. Process calls and i2c block variants +are not yet tested. + +NOTE: The "xx" in output means the byte was transferred on the +SMBus but firmware never read it out of the controller buffer. +On writes such bytes are to be considered as "no change". + +*** Build instructions + + $ make + +*** Command-line operation + +Input file is log output from serialice, stdout has +the smbus traffic translated into a more readable form. + + $ ./dump_smbus -h + +Parse SMBus access from SerialICE logfile +dump_smbus [-dhx] -f filename + -f file input serialice logfile + -d strips SMBus inb/outb + -x strips all but SMBus IO + + $ ./dump_smbus -f qemu_serialice_log.txt > filtered.log + $ diff -y -W 200 qemu_serialice_log.txt filtered.log + +Things one may want to change in config.h + + SMBUS_PCIFUNC : PCI function with SMBus BAR + SMBUS_BAR : PCI config register for SMBus BAR + +If BAR autodetect fails, set DEFAULT_SMBUS_HOST_BASE. + + +*** SerialICE runtime translation + +Copy libsimba.so into the same directory with serialice modular +lua scripts. Simulator library should load automatically, check +for keywords "SIMBA and SMBUS" in the log. The "log_smbus_io" +boolean set in serialice.lua controls whether to show individual +inb/outb to smbus host controller. + +It may be necessary to compile qemu to use a shared lua library +to be able to load libsimba.so. + + +Kyösti Mälkki +kyosti.malkki@gmail.com diff --git a/SerialICE/util/simba/config.h b/SerialICE/util/simba/config.h new file mode 100644 index 0000000..6475598 --- /dev/null +++ b/SerialICE/util/simba/config.h @@ -0,0 +1,11 @@ + + +/* Change these to match your ICH southbridge PCI function. */ + +#define SMBUS_PCIFUNC "0:1f.3" +#define SMBUS_BAR 0x20 + +#define DEFAULT_SMBUS_HOST_BASE 0x400 +#define SMBUS_HOST_SIZE 0x20 + + diff --git a/SerialICE/util/simba/i2c-i801.h b/SerialICE/util/simba/i2c-i801.h new file mode 100644 index 0000000..aaf15a0 --- /dev/null +++ b/SerialICE/util/simba/i2c-i801.h @@ -0,0 +1,152 @@ +/* + i2c-i801.c - Part of lm_sensors, Linux kernel modules for hardware + monitoring + Copyright (c) 1998 - 2002 Frodo Looijaard frodol@dds.nl, + Philip Edelbrock phil@netroedge.com, and Mark D. Studebaker + mdsxyz123@yahoo.com + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +/* + SUPPORTED DEVICES PCI ID + 82801AA 2413 + 82801AB 2423 + 82801BA 2443 + 82801CA/CAM 2483 + 82801DB 24C3 (HW PEC supported, 32 byte buffer not supported) + 82801EB 24D3 (HW PEC supported, 32 byte buffer not supported) + 6300ESB 25A4 ("") + ICH6 266A ("") + ICH7 27DA ("") + ESB2 269B ("") + ICH8 283E ("") + ICH9 2930 ("") + Tolapai 5032 ("") + ICH10 3A30 ("") + ICH10 3A60 ("") + + This driver supports several versions of Intel's I/O Controller Hubs (ICH). + For SMBus support, they are similar to the PIIX4 and are part + of Intel's '810' and other chipsets. + See the doc/busses/i2c-i801 file for details. + I2C Block Read not supported. + Block Process Call are not supported. +*/ + + +/* 82801CA is undefined before kernel 2.4.13 */ +#ifndef PCI_DEVICE_ID_INTEL_82801CA_3 +#define PCI_DEVICE_ID_INTEL_82801CA_3 0x2483 +#endif + +/* 82801DB is undefined before kernel 2.4.19 */ +#ifndef PCI_DEVICE_ID_INTEL_82801DB_3 +#define PCI_DEVICE_ID_INTEL_82801DB_3 0x24c3 +#endif + +/* 82801EB is undefined before kernel 2.4.21 */ +#ifndef PCI_DEVICE_ID_INTEL_82801EB_3 +#define PCI_DEVICE_ID_INTEL_82801EB_3 0x24d3 +#endif + +/* ESB is undefined before kernel 2.4.22 */ +#ifndef PCI_DEVICE_ID_INTEL_ESB_4 +#define PCI_DEVICE_ID_INTEL_ESB_4 0x25a4 +#endif + +/* ESB2 - Enterprise Southbridge is undefined */ +#ifndef PCI_DEVICE_ID_INTEL_ESB2_17 +#define PCI_DEVICE_ID_INTEL_ESB2_17 0x269b +#endif + +/* ICH6 is undefined */ +#ifndef PCI_DEVICE_ID_INTEL_ICH6_16 +#define PCI_DEVICE_ID_INTEL_ICH6_16 0x266a +#endif + +/* ICH7 is undefined */ +#ifndef PCI_DEVICE_ID_INTEL_ICH7_17 +#define PCI_DEVICE_ID_INTEL_ICH7_17 0x27da +#endif + +/* ICH8 is undefined */ +#ifndef PCI_DEVICE_ID_INTEL_ICH8_5 +#define PCI_DEVICE_ID_INTEL_ICH8_5 0x283e +#endif + +/* ICH9 is undefined */ +#ifndef PCI_DEVICE_ID_INTEL_ICH9_6 +#define PCI_DEVICE_ID_INTEL_ICH9_6 0x2930 +#endif + +#ifndef PCI_DEVICE_ID_INTEL_TOLAPAI_1 +#define PCI_DEVICE_ID_INTEL_TOLAPAI_1 0x5032 +#endif + +#ifndef PCI_DEVICE_ID_INTEL_ICH10_4 +#define PCI_DEVICE_ID_INTEL_ICH10_4 0x3a30 +#endif + +#ifndef PCI_DEVICE_ID_INTEL_ICH10_5 +#define PCI_DEVICE_ID_INTEL_ICH10_5 0x3a60 +#endif + +#ifdef I2C_CLIENT_PEC +#define HAVE_PEC +#endif + +/* I801 SMBus address offsets */ +#define SMBHSTSTS (0 + i801_smba) +#define SMBHSTCNT (2 + i801_smba) +#define SMBHSTCMD (3 + i801_smba) +#define SMBHSTADD (4 + i801_smba) +#define SMBHSTDAT0 (5 + i801_smba) +#define SMBHSTDAT1 (6 + i801_smba) +#define SMBBLKDAT (7 + i801_smba) +#define SMBPEC (8 + i801_smba) /* ICH4 only */ +#define SMBAUXSTS (12 + i801_smba) /* ICH4 only */ +#define SMBAUXCTL (13 + i801_smba) /* ICH4 only */ + +/* PCI Address Constants */ +#define SMBBA 0x020 +#define SMBHSTCFG 0x040 +#define SMBREV 0x008 + +/* Host configuration bits for SMBHSTCFG */ +#define SMBHSTCFG_HST_EN 1 +#define SMBHSTCFG_SMB_SMI_EN 2 +#define SMBHSTCFG_I2C_EN 4 + +/* Other settings */ +#define MAX_TIMEOUT 100 +#define ENABLE_INT9 0 /* set to 0x01 to enable - untested */ + +/* I801 command constants */ +#define I801_QUICK 0x00 +#define I801_BYTE 0x04 +#define I801_BYTE_DATA 0x08 +#define I801_WORD_DATA 0x0C +#define I801_PROC_CALL 0x10 /* later chips only, unimplemented */ +#define I801_BLOCK_DATA 0x14 +#define I801_I2C_BLOCK_DATA 0x18 /* unimplemented */ +#define I801_BLOCK_LAST 0x34 +#define I801_I2C_BLOCK_LAST 0x38 /* unimplemented */ +#define I801_START 0x40 +#define I801_PEC_EN 0x80 /* ich4 and later */ + + + + diff --git a/SerialICE/util/simba/i82801_sim.c b/SerialICE/util/simba/i82801_sim.c new file mode 100644 index 0000000..0bda7c2 --- /dev/null +++ b/SerialICE/util/simba/i82801_sim.c @@ -0,0 +1,493 @@ +/* + * Copyright (C) 2012 Kyösti Mälkki kyosti.malkki@gmail.com + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> +#include <stdlib.h> + +/* Intel 82801 SMBus Controller defines from linux src. */ +#include "i2c-i801.h" + +/* complement i2c-i801.c */ +#define i801_smba 0 +#define I801_BLOCK_PROCESS 0x1c + +#include "simba.h" + +static struct smbus_ctrl i801_smbus_controller; +static struct smbus_ctrl *host = &i801_smbus_controller; + +static unsigned int smbus_host_base = 0; +static unsigned int smbus_host_size = 0; +static int passive_mode = 1; + +/* test for !passive_mode */ +void smbus_transaction(struct smbus_ctrl *host) +{ + int i; + unsigned char smb_cmd = (host->control & 0x1c); + + if (smb_is_read(host)) { + for (i=0; i<host->data0; i++) { + host->block_sram[i].hw = 0x80 + i; + host->block_sram[i].hw_valid = 1; + } + } + /* Finish all */ + host->status &= ~0x01; + host->status |= 0x02; +} + +static int smbus_completed(void); + + +static void smbus_state_switch(smb_state_t new_state) +{ + unsigned char smb_cmd = (host->control & 0x1c); + int i; + + if (new_state == host->state) { + dprintf("SMBus: state switch without switch\n"); + return; + } + + switch (new_state) { + case HOST_NOOP: + dprintf("SMBus: state switch to HOST_NOOP\n"); + new_state = HOST_IDLE_0; + break; + + case HOST_IDLE_0: + break; + + case HOST_ACTIVE_0: + /* INUSE_STS is set on SMBHSTSTS read */ + host->status = 0x0; + break; + + case HOST_ACTIVE_1: + { + host->status |= 0x01; + host->wr_data0 = host->data0; + host->wr_data1 = host->data1; + if (smb_is_read(host)) { + host->data0_valid = 0; + host->data1_valid = 0; + } + for (i=0; i<MAX_BLOCK_SRAM; i++) { + /* On block writes, previously read data in buffer is also valid. */ + if (host->block_sram[i].tmp_valid) { + host->block_sram[i].hw = host->block_sram[i].tmp; + host->block_sram[i].tmp_valid = 0; + host->block_sram[i].hw_valid = 1; + } + /* On block reads, no data in buffer is yet valid. */ + if (smb_is_read(host) && smb_cmd == I801_BLOCK_DATA) + host->block_sram[i].hw_valid = 0; + } + break; + } + + case HOST_DONE_0: + if (!smbus_completed()) + host->next_state = HOST_WAIT_0; + else + host->next_state = HOST_DONE_1; + break; + + case HOST_DONE_2: + host->next_state = host->state_completion; + break; + + case HOST_FAIL_0: + host->next_state = HOST_ACTIVE_0; + break; + + default: + break; + } + + host->new_state = new_state; +} + +static void smbus_state_run(void) +{ + smb_state_t next_state; + + while (host->state != host->new_state) { + dprintf("SMBus: state change %s -> %s\n", + ctrl_state[host->state], ctrl_state[host->new_state]); + host->state = host->new_state; + + switch (host->state) { + case HOST_DONE_1: + case HOST_DONE_2: + case HOST_FAIL_0: + smbus_dump_transaction(host); + break; + } + + next_state = host->next_state; + host->next_state = HOST_NOOP; + if (next_state != HOST_NOOP) + smbus_state_switch(next_state); + } +} + +static int smbus_completed(void) +{ + int complete = 0; + + if (!smb_is_read(host)) + return 1; + + switch (get_cmdcode(host)) { + + case SMBUS_QUICK: + complete = 1; + break; + + case SMBUS_BYTE: + complete = host->data0_valid; + break; + + case SMBUS_BYTE_DATA: + complete = host->data0_valid; + break; + + case SMBUS_WORD_DATA: + complete = host->data0_valid && host->data1_valid; + break; + + case SMBUS_BLOCK_DATA: + complete = host->data0_valid && (host->block_ptr >= host->data0); + break; + + case SMBUS_PROC_CALL: + case SMBUS_I2C_BLOCK_DATA: + case SMBUS_BLOCK_PROCESS: + default: + printf("SMBus (cmd_%02x): Unimplemented completion\n", host->smb_cmd); + break; + + } + return complete; +} + +static void smbus_signal_status(unsigned char data, unsigned char io_write) +{ + int reset_inuse = (data & 0x40); + int reset_intr = (data & 0x02); + +#if 0 + if (reset_inuse && (host->state != HOST_DONE_1)) + dprintf("SMBus: Premature reset of bit INUSE_STS\n"); +#endif + + switch (host->state) { + + case HOST_DONE_1: + if (reset_inuse && reset_intr) + smbus_state_switch(HOST_IDLE_0); + else if (reset_intr) + smbus_state_switch(HOST_ACTIVE_0); + break; + + case HOST_WAIT_0: + if (reset_inuse && reset_intr) { + host->state_completion = HOST_IDLE_0; + smbus_state_switch(HOST_WAIT_1); + } else if (reset_intr) { + host->state_completion = HOST_ACTIVE_0; + smbus_state_switch(HOST_WAIT_1); + } + break; + + case HOST_WAIT_1: + if (reset_intr) + smbus_state_switch(HOST_DONE_2); + break; + + default: + break; + } + +} + +static void smbus_signal_start(void) +{ + switch (host->state) { + case HOST_IDLE_0: + case HOST_ACTIVE_0: + smbus_state_switch(HOST_ACTIVE_1); + break; + + default: + printf("SMBus: Starting from illegal state\n"); + break; + } + + switch (host->control & 0x1c) { + + case I801_QUICK: + set_cmdcode(host, SMBUS_QUICK); + break; + + case I801_BYTE: + set_cmdcode(host, SMBUS_BYTE); + break; + + case I801_BYTE_DATA: + set_cmdcode(host, SMBUS_BYTE_DATA); + break; + + case I801_WORD_DATA: + set_cmdcode(host, SMBUS_WORD_DATA); + break; + + case I801_PROC_CALL: + set_cmdcode(host, SMBUS_PROC_CALL); + break; + + case I801_BLOCK_DATA: + set_cmdcode(host, SMBUS_BLOCK_DATA); + break; + + case I801_I2C_BLOCK_DATA: + set_cmdcode(host, SMBUS_I2C_BLOCK_DATA); + break; + + case I801_BLOCK_PROCESS: + set_cmdcode(host, SMBUS_BLOCK_PROCESS); + break; + + default: + printf("SMBus: Unknown controlcode\n"); + break; + } + +} + +/* + * + */ +static void smbus_host_status(unsigned char * data, unsigned char io_write) +{ + if (!io_write) { + if (passive_mode) + host->status = *data; + + switch (host->state) { + + case HOST_IDLE_0: + if (! (host->status & 0x40)) + smbus_state_switch(HOST_ACTIVE_0); + break; + + case HOST_ACTIVE_1: + if ((!host->status & 0x02) && (!passive_mode)) + smbus_transaction(host); + if (host->status & 0x1c) + smbus_state_switch(HOST_FAIL_0); + else if ((host->status & 0x02) && !(host->status & 0x01)) + smbus_state_switch(HOST_DONE_0); + break; + } + + if (!passive_mode) { + *data = host->status; + host->status |= 0x40; + } + } else { + if (!passive_mode) + host->status &= ~(*data); + + smbus_signal_status(*data, io_write); + } + +} + + +static void smbus_host_control(unsigned char * data, unsigned char io_write) +{ + if (!io_write) { + host->block_ptr=0; + if (!passive_mode) + *data = host->control & ~0x40; + } else { + host->control = *data; + if (host->control & 0x80) + printf("SMBus: No PEC simulation\n"); + if (host->control & 0x40) + smbus_signal_start(); + } +} + + +static void smbus_block_data(unsigned char * data, unsigned char io_write) +{ + if (passive_mode) { + if (host->block_ptr < MAX_BLOCK_SRAM) { + if (!io_write) { + host->block_sram[host->block_ptr].hw = *data; + host->block_sram[host->block_ptr].hw_valid = 1; + } + host->block_sram[host->block_ptr].tmp = *data; + host->block_sram[host->block_ptr].tmp_valid = 1; + } + } else { + if (host->block_ptr < MAX_BLOCK_SRAM) { + if (io_write) { + host->block_sram[host->block_ptr].tmp = *data; + host->block_sram[host->block_ptr].tmp_valid = 1; + } else { + *data = 0xff; + if (host->block_sram[host->block_ptr].tmp_valid) + *data = host->block_sram[host->block_ptr].tmp; + else if (host->block_sram[host->block_ptr].hw_valid) + *data = host->block_sram[host->block_ptr].hw; + } + } + } + host->block_ptr++; +} + + + +static void i82801_smbus_pre_io(unsigned char reg, unsigned char data, unsigned char io_write) +{ + switch (host->state) { + case HOST_WAIT_1: + /* Restarting previous transaction. */ + if (io_write && reg == SMBHSTCNT && data & 0x40) + smbus_state_switch(HOST_DONE_2); + + /* Programming new transaction. */ + if (io_write && reg >= SMBHSTCMD && reg <= SMBHSTDAT1) + smbus_state_switch(HOST_DONE_2); + + /* FIXME: Killing on-going transaction. */ + if (io_write && reg == SMBHSTCNT && data & 0x02) + smbus_state_switch(HOST_DONE_2); + break; + } + smbus_state_run(); +} + +static void i82801_smbus_post_io(unsigned char reg, unsigned char data, unsigned char io_write) +{ + switch (host->state) { + case HOST_WAIT_1: + if (smbus_completed()) + smbus_state_switch(HOST_DONE_2); + break; + } + smbus_state_run(); +} + +static void i82801_smbus_io(unsigned char reg, unsigned char * data, unsigned char io_write) +{ + /* In passive mode, data registers update both ways. */ + int data_write = io_write || passive_mode; + + switch (reg) { + + case SMBHSTSTS: + smbus_host_status(data, io_write); + break; + + case SMBHSTCNT: + smbus_host_control(data, io_write); + break; + + case SMBHSTCMD: + if (data_write) + host->command = *data; + else + *data = host->command; + break; + + case SMBHSTADD: + if (data_write) + host->slave = *data; + else + *data = host->slave; + break; + + case SMBHSTDAT0: + if (data_write) + host->data0 = *data; + else + *data = host->data0; + host->data0_valid = 1; + break; + + case SMBHSTDAT1: + if (data_write) + host->data1 = *data; + else + *data = host->data1; + host->data1_valid = 1; + break; + + case SMBBLKDAT: + smbus_block_data(data, io_write); + break; + + case SMBAUXCTL: + if (data_write) + host->aux_ctl = *data; + else + *data = host->aux_ctl; + break; + + default: + printf("SMBus: Unknown register 0x%02x\n", reg); + break; + } + smbus_state_run(); +} + +int serialice_smbus_io_access(unsigned int addr, unsigned int data, int io_write, int len) +{ + unsigned char reg = addr & (host->size - 1); + unsigned char cdata = data; + + i82801_smbus_pre_io(reg, data, io_write); + i82801_smbus_io(reg, (unsigned char*)&data, io_write); + i82801_smbus_post_io(reg, data, io_write); + return 0; +} + +void serialice_smbus_dump_output(void) +{ + if (output_complete) { + output_complete = 0; + printf("SMBus-00: %s", output_buffer); + } +} + +void serialice_smbus_init(unsigned int base, unsigned int size) +{ + host->base = base & ~(size-1); + host->size = size; + dprintf("SMBus: base=0x%08x size=%04x\n", host->base, host->size); + if (output_buffer == NULL) { + output_buffer = calloc(1, 4096); + } + smbus_state_switch(HOST_IDLE_0); +} + diff --git a/SerialICE/util/simba/io_hooks.c b/SerialICE/util/simba/io_hooks.c new file mode 100644 index 0000000..58a9abd --- /dev/null +++ b/SerialICE/util/simba/io_hooks.c @@ -0,0 +1,167 @@ +/* + * Copyright (C) 2012 Kyösti Mälkki kyosti.malkki@gmail.com + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> +#include <stdlib.h> +#include <libio.h> +#include <string.h> +#include <unistd.h> + +#include "simba.h" +#include "io_hooks.h" +#include "config.h" + +static unsigned int smbus_host_base = 0; +static unsigned int smbus_host_size = 0; + +union cfg_io_register { + unsigned char r8[4]; + unsigned short r16[2]; + unsigned int r32; +}; + +typedef struct { + union cfg_io_register valid; + union cfg_io_register data; +} pci_config_reg; + +#define PCI_V23_SPACE 0x40 + +typedef struct { + char pcifunc[10]; + pci_config_reg map[PCI_V23_SPACE]; +} pci_config_space; + +pci_config_space tmpdev; + +pci_config_space sb_lpc = { + .pcifunc = SMBUS_PCIFUNC, +// .parser = sb_lpc_parser, +}; + +static pci_config_space * pci_cfg_get_bdf(char *newfunc) +{ + int i; + + if (strncmp(tmpdev.pcifunc, newfunc, sizeof(tmpdev.pcifunc))==0) + return &tmpdev; + + if (strncmp(sb_lpc.pcifunc, newfunc, sizeof(sb_lpc.pcifunc))==0) + return &sb_lpc; + + strncpy(tmpdev.pcifunc, newfunc, sizeof(tmpdev.pcifunc)); + + for (i=0; i<PCI_V23_SPACE; i++) + tmpdev.map[i].valid.r32=0; + return &tmpdev; +} + +static void pci_cfg_reg_update(pci_config_space *cfg, unsigned int reg, char len, unsigned int data) +{ + if ((reg>>2) >= PCI_V23_SPACE) + return; + + unsigned char ireg = reg & 0x03; + union cfg_io_register * rvalid = &(cfg->map[reg>>2].valid); + union cfg_io_register * rdata = &(cfg->map[reg>>2].data); + + switch (len) { + case 8: + rvalid->r8[ireg] |= 0xff; + rdata->r8[ireg] = data & 0xff; + break; + case 16: + if (ireg & 0x01) + dprintf("PCI: Bad alignment for word at %04x\n", reg); + ireg &= 0x2; + rvalid->r16[ireg] |= 0xffff; + rdata->r16[ireg] = data & 0xffff; + break; + case 32: + if (ireg & 0x03) + dprintf("PCI: Bad alignment for long at %04x\n", reg); + rvalid->r32 = 0xffffffff; + rdata->r32 = data; + break; + default: + break; + } +} + +static void pci_cfg_update(pci_config_space *cfg, unsigned int reg, unsigned int data, char io_write, char len) +{ + int vid=0, did=0; + const char *prefix[2] = {"PCI", "SubSystem" }; + pci_config_reg *pci_id; + + pci_cfg_reg_update(cfg, reg, len, data); + + if ((reg>=0x0 && reg<0x4) || (reg>=0x2c && reg<0x30)) + { + int subsys = reg>4; + pci_id = &cfg->map[reg>>2]; + vid = pci_id->valid.r8[0] && pci_id->valid.r8[1] && (pci_id->data.r16[0]!=0xffff); + did = pci_id->valid.r8[2] && pci_id->valid.r8[3] && (pci_id->data.r16[1]!=0xffff); + + if (vid && did) + dprintf("PCI_trigger: %s Vendor:Device ID = %04x:%04x\n", prefix[subsys], pci_id->data.r16[0], pci_id->data.r16[1]); + else if (vid) + dprintf("PCI_trigger: %s Vendor ID = %04x\n", prefix[subsys], pci_id->data.r16[0]); + else if (did) + dprintf("PCI_trigger: %s Device ID = %04x\n", prefix[subsys], pci_id->data.r16[1]); + } + + /* Catch SMBus Host BAR */ + if (cfg == &sb_lpc && reg>=SMBUS_BAR && reg<SMBUS_BAR+3) { + union cfg_io_register * rvalid = &cfg->map[reg>>2].valid; + union cfg_io_register * rdata = &cfg->map[reg>>2].data; + if (rvalid->r8[0] && rvalid->r8[1]) { + smbus_host_base = rdata->r16[0] & ~(SMBUS_HOST_SIZE-1); + smbus_host_size = SMBUS_HOST_SIZE; + serialice_smbus_init(smbus_host_base, smbus_host_size); + } + } +} + +int handle_pci(char *pcifunc, unsigned int reg, unsigned int data, char io_write, char len) +{ + int done=0; + + pci_config_space * cfg = pci_cfg_get_bdf(pcifunc); + pci_cfg_update(cfg, reg, data, io_write, len); + + return 0; +} + +int handle_io(unsigned int addr, unsigned int data, char io_write, int len) +{ + int done=0; + if ((addr >= smbus_host_base) && (addr < (smbus_host_base + smbus_host_size))) { + serialice_smbus_io_access(addr, data, io_write, len); + done = 1; + serialice_smbus_dump_output(); + } + return done; +} + +void smbus_defaults_init(void) +{ + smbus_host_base = DEFAULT_SMBUS_HOST_BASE; + smbus_host_size = SMBUS_HOST_SIZE; + serialice_smbus_init(smbus_host_base, smbus_host_size); +} + diff --git a/SerialICE/util/simba/io_hooks.h b/SerialICE/util/simba/io_hooks.h new file mode 100644 index 0000000..3289aa7 --- /dev/null +++ b/SerialICE/util/simba/io_hooks.h @@ -0,0 +1,6 @@ + + +void smbus_defaults_init(void); + +int handle_pci(char *pcifunc, unsigned int reg, unsigned int data, char io_write, char len); +int handle_io(unsigned int addr, unsigned int data, char io_write, int len); diff --git a/SerialICE/util/simba/main.c b/SerialICE/util/simba/main.c new file mode 100644 index 0000000..bdde449 --- /dev/null +++ b/SerialICE/util/simba/main.c @@ -0,0 +1,157 @@ +/* + * Copyright (C) 2012 Kyösti Mälkki kyosti.malkki@gmail.com + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> +#include <stdlib.h> +#include <libio.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> + +#include "io_hooks.h" + +/* serialice log parser */ +static char line[80]; +static char s_cmd[3]=""; +static char s_pcidev[10]=""; +static char s_addr[]="0x00000000"; +static char s_data[]="0x00000000"; +static char s_reg[]="0x00"; + +static int try_parse_io(const char * line) +{ + unsigned int addr, data; + int valid=0, len=0; + + valid = sscanf(line, "IO: %*4s %8s %2s %8s", &s_addr[2], &s_cmd[0], &s_data[2]) == 3; + if (!valid) + return 0; + + valid &= (sscanf(s_addr, "%x", &addr)==1); + valid &= (sscanf(s_data, "%x", &data)==1); + len = 4 * (strlen(s_data) - 2); + + if (valid && strcmp("=>", s_cmd)==0) + valid = handle_io(addr, data, 0, len); + else if (valid && strcmp("<=", s_cmd)==0) + valid = handle_io(addr, data, 1, len); + + return valid; +} + +static int try_parse_pci(const char * line) +{ + unsigned int addr, data, reg; + int valid=0, len=0; + + valid = sscanf(line, "PCI %6s R.%2s %2s %8s", &s_pcidev[0], &s_reg[2], &s_cmd[0], &s_data[2]) == 4; + if (!valid) + return 0; + + valid &= (sscanf(s_reg, "%x", ®)==1); + valid &= (sscanf(s_data, "%x", &data)==1); + len = 4 * (strlen(s_data) - 2); + if (valid && strcmp("=>", s_cmd)==0) + valid = handle_pci(s_pcidev, reg, data, 0, len); + else if (valid && strcmp("<=", s_cmd)==0) + valid = handle_pci(s_pcidev, reg, data, 1, len); + + return valid; +} + + +void usage(void) +{ + printf( "Parse SMBus access from SerialICE logfile\n" + "dump_smbus [-dhx] -f filename\n" + "\t-f file input serialice logfile\n" + "\t-d strips SMBus inb/outb\n" + "\t-x strips all but SMBus IO\n"); +} + +int main(int argc, char *argv[]) +{ + char * filename = NULL; + int show_smbus_only = 0; + int show_smbus_io = 1; + int c; + FILE *txtlog; + + while ((c = getopt (argc, argv, "df:hx")) != -1) { + switch (c) { + case 'd': + show_smbus_io = 0; + break; + case 'f': + filename = optarg; + break; + case 'h': + usage(); + exit(0); + break; + case 'x': + show_smbus_only = 1; + break; + case '?': + if (optopt == 'f') + fprintf (stderr, "Option -%c requires an argument.\n", optopt); + else if (isprint (optopt)) + fprintf (stderr, "Unknown option `-%c'.\n", optopt); + else + fprintf (stderr, "Unknown option character `\x%x'.\n", optopt); + return 1; + default: + abort (); + } + } + + if (! filename) { + usage(); + exit(1); + } + + txtlog = fopen(filename, "r"); + if (! txtlog) { + printf("File %s : %s\n", filename, strerror(errno)); + exit(2); + } + + smbus_defaults_init(); + + while (!feof(txtlog)) { + int done = 0, show = 0; + + fgets(line, sizeof(line), txtlog); + + if (!done) { + done = try_parse_io(line); + if (done && show_smbus_io) + fputs(line, stdout); + } + + if (!done) { + done = try_parse_pci(line); + if (done && !show_smbus_only) + fputs(line, stdout); + } + continue; + } + + fclose(txtlog); + exit(0); +} + diff --git a/SerialICE/util/simba/simba.c b/SerialICE/util/simba/simba.c new file mode 100644 index 0000000..7714e11 --- /dev/null +++ b/SerialICE/util/simba/simba.c @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2012 Kyösti Mälkki kyosti.malkki@gmail.com + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> +#include <string.h> +#include "simba.h" + +char * output_buffer = NULL; +int output_complete = 0; + +int smbus_dump_block(char *s, struct smbus_ctrl *host) +{ + int i, j=0, k=0; + for (i=0; i<host->data0 && i<MAX_BLOCK_SRAM; i++) { + if (host->block_sram[i].hw_valid) { + j = sprintf(s, " %02x", host->block_sram[i].hw); + } else { + j = sprintf(s, " xx"); + } + s += j; + k += j; + } + return k; +} + +void smbus_dump_transaction(struct smbus_ctrl *host) +{ + const char invalid[] = "xx"; + const char *iodir[2] = { "<=", "=>" }; + const char *dir = smb_is_read(host) ? iodir[1] : iodir[0]; + char data0[4], data1[4]; + char * s = output_buffer; + int length; + + output_complete = 1; + + if (host->data0_valid) { + length = host->data0; + sprintf(data0, "%02x", host->data0); + } else { + length = 0; + strncpy(data0, invalid, sizeof(data0)); + } + + if (host->data1_valid) + sprintf(data1, "%02x", host->data1); + else + strncpy(data1, invalid, sizeof(data1)); + + s += sprintf(s, "%02x proto=%02x ", smb_client(host), get_cmdcode(host)); + + if (host->state == HOST_FAIL_0) { + s += sprintf(s, "Failed transaction (%s)\n", dir); + return; + } + + switch (get_cmdcode(host)) { + + case SMBUS_QUICK: + s += sprintf(s, "\n"); + break; + + case SMBUS_BYTE: + s += sprintf(s, "%02x %s %s\n", host->command, dir, data0); + break; + + case SMBUS_BYTE_DATA: + s += sprintf(s, "%02x %s %s\n", host->command, dir, data0); + break; + + case SMBUS_WORD_DATA: + s += sprintf(s, "%02x %s %s%s\n", host->command, dir, data0, data1); + break; + + case SMBUS_PROC_CALL: + s += sprintf(s, "%02x %02x %02x %s %s %s\n", + host->command, host->wr_data0, host->wr_data1, + iodir[1], data0, data1); + break; + + case SMBUS_BLOCK_DATA: + s += sprintf(s, "%02x len=%02d %s", host->command, length, dir); + s += smbus_dump_block(s, host); + s += sprintf(s, "\n"); + break; + + case SMBUS_I2C_BLOCK_DATA: + s += sprintf(s, "%02x %02x %02x len=%02d %s", + host->command, host->data0, host->data1, host->block_ptr, dir); + s += smbus_dump_block(s, host); + s += sprintf(s, "\n"); + break; + + case SMBUS_BLOCK_PROCESS: + s += sprintf(s, "%02x len=%02d %s", host->command, length, iodir[1]); + s += smbus_dump_block(s, host); + s += sprintf(s, "\n"); + break; + + default: + s += sprintf(s, "Cannot parse command\n"); + break; + } +} + diff --git a/SerialICE/util/simba/simba.h b/SerialICE/util/simba/simba.h new file mode 100644 index 0000000..af64eba --- /dev/null +++ b/SerialICE/util/simba/simba.h @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2012 Kyösti Mälkki kyosti.malkki@gmail.com + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +/* Our SMBus Host Simulator states. */ +typedef enum { + HOST_NOOP, + HOST_IDLE_0, + HOST_ACTIVE_0, + HOST_ACTIVE_1, + HOST_DONE_0, + HOST_DONE_1, + HOST_DONE_2, + HOST_WAIT_0, + HOST_WAIT_1, + HOST_FAIL_0, +} smb_state_t; + +static const char *ctrl_state[] = { + "noop", + "idle_0", + "active_0", + "active_1", + "done_0", + "done_1", + "done_2", + "wait_0", + "wait_1", + "fail_0", +}; + +typedef enum { + SMBUS_NOOP, + SMBUS_QUICK, + SMBUS_BYTE, + SMBUS_BYTE_DATA, + SMBUS_WORD_DATA, + SMBUS_PROC_CALL, + SMBUS_BLOCK_DATA, + SMBUS_I2C_BLOCK_DATA, + SMBUS_BLOCK_PROCESS, +} smb_cmd_t; + + +#define MAX_BLOCK_SRAM 32 + + +struct sram_buf { + unsigned char hw; + unsigned char hw_valid; + unsigned char tmp; + unsigned char tmp_valid; +}; + +struct smbus_ctrl { + + /* IO region */ + unsigned int base; + unsigned int size; + + /* HW registers */ + unsigned char status; + unsigned char control; + unsigned char command; + unsigned char slave; + unsigned char data0; + unsigned char data1; + unsigned char aux_ctl; + + unsigned char data0_valid; + unsigned char data1_valid; + + /* for state machine */ + smb_state_t state; + smb_state_t new_state; + smb_state_t next_state; + smb_state_t state_completion; + smb_cmd_t smb_cmd; + + /* for E32B */ + unsigned char block_ptr; + unsigned char max_block_ptr; + struct sram_buf block_sram[MAX_BLOCK_SRAM]; + + /* for Process Call */ + unsigned char wr_data0; + unsigned char wr_data1; +}; + + +#if DEBUG +#define dprintf(x, ...) do { printf(x, ## __VA_ARGS__); } while (0) +#else +#define dprintf(x, ...) do { } while (0) +#endif + + +#define smb_is_read(x) (x->slave & 0x01) +#define smb_client(x) (x->slave >> 1) + +static inline void set_cmdcode(struct smbus_ctrl *host, int code) +{ + host->smb_cmd = (code<<1) | smb_is_read(host); +} + +static inline int get_cmdcode(struct smbus_ctrl *host) +{ + return (host->smb_cmd>>1); +} + +extern char * output_buffer; +extern int output_complete; +int smbus_dump_block(char * s, struct smbus_ctrl *host); +void smbus_dump_transaction(struct smbus_ctrl *host); +void smbus_dump_io(unsigned char io_write, unsigned int reg, unsigned char pre_data, unsigned char post_data); + +void serialice_smbus_init(unsigned int base, unsigned int size); +void serialice_smbus_dump_output(void); +int serialice_smbus_io_access(unsigned int addr, unsigned int data, int io_write, int len); + diff --git a/SerialICE/util/simba/simba_lua.c b/SerialICE/util/simba/simba_lua.c new file mode 100644 index 0000000..a35d26c --- /dev/null +++ b/SerialICE/util/simba/simba_lua.c @@ -0,0 +1,70 @@ + + +#include <lua.h> +#include <lauxlib.h> +#include "simba.h" + +static int l_smbus_get_output(lua_State *L) +{ + const char * s = output_buffer; + if (output_complete) { + output_complete = 0; + lua_pushinteger(L, 0); + lua_pushstring(L, s); + } else { + lua_pushinteger(L, -1); + lua_pushinteger(L, -1); + } + return 2; +} + +static int l_smbus_read_log(lua_State *L) +{ + unsigned int addr = lua_tonumber(L, 1); + unsigned int size = lua_tonumber(L, 2); + unsigned int data = lua_tonumber(L, 3); + serialice_smbus_io_access(addr, data, 0, size); + return 0; +} + +static int l_smbus_write_log(lua_State *L) +{ + unsigned int addr = lua_tonumber(L, 1); + unsigned int size = lua_tonumber(L, 2); + unsigned int data = lua_tonumber(L, 3); + serialice_smbus_io_access(addr, data, 1, size); + return 0; +} + +static const struct luaL_reg smbus_hostsim_lib [] = { + {"get_output", l_smbus_get_output}, + {"read_log", l_smbus_read_log}, + {"write_log", l_smbus_write_log}, + {NULL, NULL} /* sentinel */ +}; + +static int l_smbus_init(lua_State *L) +{ + unsigned int base = lua_tonumber(L, 1); + unsigned int size = lua_tonumber(L, 2); + + serialice_smbus_init(base, size); + luaL_openlib(L, "smbus_host", smbus_hostsim_lib, 0); + return 1; +} + +/* + * + */ + +static const struct luaL_reg simba_lib [] = { + {"smbus_init", l_smbus_init}, + {NULL, NULL} /* sentinel */ +}; + +int luaopen_simba(lua_State *L) +{ + luaL_openlib(L, "simba", simba_lib, 0); + return 1; +} +